标签:
杭电hdu上的链接http://acm.hdu.edu.cn/showproblem.php?pid=1002
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] != ‘\0‘) { 22 a[i] = sza[i] - ‘0‘; 23 i++; 24 } 25 i = 0; 26 while(szb[i] != ‘\0‘) { 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:\n",count); 70 // cout << "Case";//此处考虑到用cout输出需要的行数多,故使用printf输出 71 // cout << count; 72 // cout << ":\n"; 73 int i; 74 i = 0; 75 while(sza[i]!=‘\0‘) { 76 printf("%c",sza[i]); 77 i++; 78 } 79 cout << " + "; 80 i = 0; 81 while(szb[i]!=‘\0‘) { 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 }
方法仅供参考,我知道肯定不是最简单的方法,有时间会再改善的,谢谢码友支持,欢迎评论。
标签:
原文地址:http://www.cnblogs.com/Ddlm2wxm/p/5701190.html