杭电oj 1002

 1 #include <iostream>

 2 #include <algorithm>

 3 using namespace std;

 4 int nCases;

 5 int m[1001], n[1001];

 6 char a[1001], b[1001];

 7 int main()

 8 {

 9     scanf("%d", &nCases);

10     for(int i = 1; i <= nCases; ++i)

11     {

12         memset(m, 0, sizeof(m));

13         memset(n, 0, sizeof(n));

14         getchar();

15         scanf("%s %s", a, b);

16         int len1 = strlen(a);

17         int len2 = strlen(b);

18         if(len1 > len2)   //是m长度大于n

19         {

20             // 把字符数组a逆序转成int数组m

21             int cnt = 0;

22             for(int j = len1-1; j >= 0; --j)

23                 m[cnt++] = a[j] - '0';

24             // 把字符数组b逆序转成int数组n

25             cnt = 0;

26             for(int j = len2-1; j >= 0; --j)

27                 n[cnt++] = b[j] - '0';

28         }

29         else

30         {

31             // 把字符数组a逆序转成int数组n

32             int cnt = 0;

33             for(int j = len1-1; j >= 0; --j)

34                 n[cnt++] = a[j] - '0';

35             // 把字符数组b逆序转成int数组m

36             cnt = 0;

37             for(int j = len2-1; j >= 0; --j)

38                 m[cnt++] = b[j] - '0';

39         }

40         int len = len1 > len2? len2: len1;

41         int k = 0;  //指示最高位

42 

43         for(int j = 0; j < len; ++j)

44         {

45             m[j] += n[j];

46             if(m[j] >= 10)

47                 k = j+1;

48             m[j+1] += (m[j]/10);

49             m[j] %= 10;

50         }

51         printf("Case %d:\n", i);

52         printf("%s + %s = ",a, b);

53         len = len1>len2? len1: len2;

54         if(k < len-1)

55             k = len-1;

56         for(int j = k; j >= 0; --j)

57             printf("%d", m[j]);

58         i == nCases? printf("\n"): printf("\n\n");

59     }

60     return 0;

61 }

 

你可能感兴趣的:(杭电)