hdu1002

//c
//https://github.com/ssdutyuyang199401/hduacm/blob/master/1002.c
#include
#include
int shu(char a)
{
return (a-'0');
}
int main(){
char a[1000],b[1000];
int num[1001];
int n,i,j=1,al,bl,k,t;
scanf("%d",&n);
while(n--)
{
if(j!=1)
printf("\n");
scanf("%s",a);
al=strlen(a);
scanf("%s",b);
bl=strlen(b);
k=(al>bl)?al:bl;
for(i=0;i<=k;i++)
num[i]=0;
t=k;
for(k;al>0&&bl>0;k--)
{
num[k]+=shu(a[--al])+shu(b[--bl]);

       if(num[k]/10)  
       {  
           num[k-1]++;  
           num[k]%=10;  
       }  
   }  
   while(al>0)  
   {  
        num[k--]+=shu(a[--al]);  
        if(num[k+1]/10)  
       {  
           num[k]++;  
           num[k+1]%=10;  
       }  
   }  
   while(bl>0)  
   {  
        num[k--]+=shu(b[--bl]);  
        if(num[k+1]/10)  
       {  
           num[k]++;  
           num[k+1]%=10;  
       }  
   }  

   printf("Case %d:\n",j++);  
   printf("%s + %s = ",a,b);  
   for(i=0;i<=t;i++)  
   {  
       if(i==0&&num[i]==0)  
       i++;  
       printf("%d",num[i]);  
   }  
   printf("\n");  

}
return 0;
}

//c++
//https://github.com/wangkendy/hduacm/blob/master/1002.cpp
#include 
#include 
#include 
using namespace std;

class BigInt {
public:
BigInt operator+(const BigInt& rhs) const {
BigInt res;
int len1 = num.size();
int len2 = rhs.num.size();
int len = (len1 > len2) ? len1 : len2;
len += 1;
res.num.resize(len);
vector v1(len, 0);
vector v2(len, 0);
int j = len - 1;
for (int i = len1 - 1; i >= 0; i--) {
v1[j] = num[i];
j--;
}
j = len - 1;
for (int i = len2 - 1; i >= 0; i--) {
v2[j] = rhs.num[i];
j--;
}
int c= 0;
int tmp;
for (int i = len - 1; i >= 0; i--) {
tmp = v1[i] + v2[i] + c;
res.num[i] = tmp % 10;
c = tmp / 10;
}
return res;
}

friend istream& operator>>(istream& in, BigInt &op) {
    in >> op.str;
    op.num.resize(op.str.size());
    for (int i = 0; i < op.str.size(); i++)
        op.num[i] = op.str[i] - '0';
    return in;
}

friend ostream& operator<<(ostream& out, BigInt &op) {
    int i = 0;
    while (op.num[i] == 0)
        i++;
    for (; i < op.num.size(); i++)
        cout << op.num[i];
    return out;
}

private:
vector num;
string str;
};

int main()
{
int T;
cin >> T;
BigInt a, b;
BigInt res;
for (int i = 1; i <= T; i++){
cin >> a >> b;
res = a + b;
cout << "Case " << i << ":" << endl;
cout << a << " + " << b << " = " << res << endl;
if (i != T)
cout << endl;;
}
return 0;
}

//c#
//未通过,表达形式错误。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace acm
{
class Program
{

    static int shu(char a)
    {
        return (a - '0');
    }

    // static void Main(string[] args)

    static void Main(string[] args)
    {
         //char a[1000], b[1000];
         char[] numA=new char [1000] ,numB=new char [1000];
        //int num[1001];
        int[] numSum= new Int32[1001];
        int n, i, j = 1, al, bl, k, t;
        // scanf("%d", &n);
        n = Convert.ToInt32(Console.ReadLine());
        while (n-- > 0)
        {
            if (j != 1)
                // printf("\n");
                Console.Write("\n");
            string s = Console.ReadLine();
            string[] str = s.Split(' ');
            numA = str[0].ToCharArray();
            numB = str[1].ToCharArray();

            //scanf("%s", a);
            //al = strlen(a);
            //scanf("%s", b);
            //bl = strlen(b);
            al = numA.Length;
            bl = numB.Length;

            k = (al > bl) ? al : bl;
            for (i = 0; i <= k; i++)
                numSum[i] = 0;
            t = k;
            for (; al > 0 && bl > 0; k--)
            {
                numSum[k] += shu(numA[--al]) + shu(numB[--bl]);

                if (numSum[k] / 10>0)
                {
                    numSum[k - 1]++;
                    numSum[k] %= 10;
                }
            }
            while (al > 0)
            {
                numSum[k--] += shu(numA[--al]);
                if (numSum[k + 1] / 10>0)
                {
                    numSum[k]++;
                    numSum[k + 1] %= 10;
                }
            }
            while (bl > 0)
            {
                numSum[k--] += shu(numB[--bl]);
                if (numSum[k + 1] / 10>0)
                {
                    numSum[k]++;
                    numSum[k + 1] %= 10;
                }
            }

            Console.Write("Case {0}:\n", j++);
            Console.Write("{0} + {1} = ", new string(numA), new string(numB));
            for (i = 0; i <= t; i++)
            {
                if (i == 0 && numSum[i] == 0)
                    i++;
                Console.Write("{0}", numSum[i]);
            }
            Console.Write("\n");
        }
        
    }
}

}

//java

转载于:https://www.cnblogs.com/wangkun1993/p/6268990.html

你可能感兴趣的:(hdu1002)