<hdu

 杭电hdu上的链接http://acm.hdu.edu.cn/showproblem.php?pid=1002

 Problem Description:
   I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
 
 Input:
   The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
 
 Output
   For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
 
 Sample Input
   2
   1 2
   112233445566778899   998877665544332211
 
 Sample Output
  Case 1:
  1 + 2 = 3
 
  Case 2:
  112233445566778899 + 998877665544332211 = 1111111111111111110
 
  解题思路:刚开始做这道题的时候,以为是A+B的题也难不到哪去,谁知道改了好几个小时。开始试了int,不行,又改long,不行,然后改long long,还是不行。只能另外想一种方法了:用大位数加法,用数组装数字,然后从个位开始逐位相加,过十进一。开始的时候想肯定是用数组了,肯定不会是字符数组。但是做着做着发现不行,数组的长度无法确定,想起来字符数组有个strlen的用法,就改用字符数组了。
 以下是具体代码:
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #define N 1005
  5 using namespace std;
  6 
  7 int count = 0;
  8 int a[N];
  9 int b[N];
 10 int he[N];
 11 char sza[N];
 12 char szb[N];
 13 
 14 void calcuation() {
 15     int flag = 1;
 16     int len1,len2;
 17     len1 = strlen(sza);
 18     len2 = strlen(szb);
 19     int i,j,k,l;
 20     i = 0;
 21     while(sza[i] != '') {
 22         a[i] = sza[i] - '0';
 23         i++;
 24     }
 25     i = 0;
 26     while(szb[i] != '') {
 27         b[i] = szb[i] - '0';
 28         i++;
 29     }
 30     if(len1 < len2)  {l = len1-1;flag = 0;}
 31     if(len1 > len2)  {l = len2-1;flag = -1;}
 32     if(len1 == len2) l = len1-1;
 33     k = 0;
 34     int num = 0;
 35     int m,n;
 36     m=len1-1;
 37     n=len2-1;
 38     while(l >= 0) {
 39         he[k] += a[m--] + b[n--];
 40         
 41         if(he[k] >= 10) {
 42             num = he[k] / 10;
 43             he[k] = he[k] % 10;
 44             he[k+1] = num;
 45             num = 0;
 46         }
 47         k++;
 48         l--;
 49     }
 50     i = k;
 51     j = k;
 52     if(flag == 0) {//len1 < len2
 53         for(k = len2-1-i;k >= 0; k--) {
 54             he[j] += b[k];
 55             j++;
 56         }
 57     }
 58     if(flag == -1) {//len1 > len2
 59         for(k = len1-1-i;k >= 0; --k) {
 60             he[j] += a[k];
 61             j++;
 62         }
 63     }
 64 }
 65 
 66 void find_print() {
 67     int loc;
 68     calcuation();
 69     printf("Case %d:
",count);
 70 //    cout << "Case";//此处考虑到用cout输出需要的行数多,故使用printf输出
 71 //    cout << count;
 72 //    cout << ":
";
 73     int i;
 74     i = 0;
 75     while(sza[i]!='') {
 76         printf("%c",sza[i]);
 77         i++;
 78     }
 79     cout << " + ";
 80     i = 0;
 81     while(szb[i]!='') {
 82         printf("%c",szb[i]);
 83         i++;
 84     }
 85     cout << " = ";
 86     for(i = N; i >= 0; i--) {
 87         if(he[i] != 0) {
 88             loc = i;
 89             break;
 90         }
 91     }
 92     for(i = loc;i >= 0; i--) {
 93         printf("%d",he[i]);
 94     }
 95     cout << endl;
 96 }
 97 
 98 int main() {
 99     int n;
100     cin >> n;
101     while(n--) {
102         count++;
103         memset(he,0,sizeof(he));
104         memset(a,0,sizeof(a));
105         memset(b,0,sizeof(b));
106         cin >> sza >> szb;
107         find_print();
108         if(n != 0)///根据题意最后用换行
109             cout << endl;
110     }
111     return 0;
112 }
View Code

  方法仅供参考,我知道肯定不是最简单的方法,有时间会再改善的,谢谢码友支持,欢迎评论。

原文地址:https://www.cnblogs.com/Ddlm2wxm/p/5701190.html