码迷,mamicode.com
首页 > 其他好文 > 详细

hdu1002

时间:2017-01-10 14:41:52      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:cas   case   nbsp   cout   amp   main   错误   vector   .com   

//c
//https://github.com/ssdutyuyang199401/hduacm/blob/master/1002.c
#include<stdio.h>
#include<string.h>
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

hdu1002

标签:cas   case   nbsp   cout   amp   main   错误   vector   .com   

原文地址:http://www.cnblogs.com/wangkun1993/p/6268990.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!