HDU A + B Problem II 大数据

 1 import static java.lang.System.out;
 2 import java.math.BigInteger;
 3 import java.util.Scanner;
 4 
 5 public class Main {
 6 
 7     public static void main(String[] args) {
 8         BigInteger a, b;
 9         Scanner sc = new Scanner(System.in);
10         int T = sc.nextInt();
11         for (int i = 1; i <= T; i++) {
12             a = sc.nextBigInteger();
13             b = sc.nextBigInteger();
14             out.println("Case " + i + ":");
15             out.println(a + " + " + b + " = " + a.add(b));
16             if (i < T)
17                 out.println();
18         }
19     }
20 }
View Code

java中更加快捷

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #define sc(x) scanf("%d",&x)
 6 #define pf(x) printf("%d\n",x)
 7 #define PF(x) printf("%d",x)
 8 #define CL(x,y) memset(x,y,sizeof(x))
 9 using namespace std;
10 const int MAX = 1005;
11 char a[MAX], b[MAX];
12 int aLen, bLen, n;
13 int T, k, i, j;
14 void Add(char* a, char* b);
15 int main()
16 {
17     sc(T);
18     for(i = 1; i <= T; i++)
19     {
20         CL(a, 0);
21         CL(b, 0);
22         cin >> a >> b;
23         printf("Case %d:\n", i);
24         cout << a << " + " << b << " = ";
25         Add(a, b);
26         if(i < T) printf("\n");
27     }
28     return 0;
29 }
30 void Add(char* a, char* b)
31 {
32     aLen = strlen(a);
33     bLen = strlen(b);
34     n = MAX - (aLen > bLen ? aLen : bLen);//设置等位线
35     for(j = aLen-1, k = MAX; j >= 0; --j, --k)
36     {
37         a[k] = a[j]-'0';
38         a[j] = 0;
39     }
40     for(j = bLen-1, k = MAX; j >= 0; --j, --k)
41     {
42         b[k] = b[j]-'0';
43         b[j] = 0;
44     }
45     for(k = MAX; k >= n; --k)
46         a[k] += b[k];
47     for(k = MAX; k >= n; --k)
48     {
49         int temp = a[k]/10;//进位
50         a[k-1] += temp;
51         a[k] %= 10;
52     }
53     for(k = n+1; k <= MAX; k++)
54 //            cout << a[k] << endl;
55         PF(a[k]);
56         cout << endl;
57 }
View Code

 

posted @ 2015-03-11 14:01  PastLIFE  阅读(140)  评论(0编辑  收藏  举报