本题大意:给出两个1000位以内的大数a 和b,让你计算a + b的值。
本题思路:字符串模拟就能过,会Java的大佬应该不会点进来......
参考代码:
1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 5 const int maxn = 1000 + 5; 6 int t, Case = 0, now; 7 int ans[maxn]; 8 char s1[maxn], s2[maxn]; 9 10 int main () { 11 scanf("%d", &t); 12 while(t --) { 13 if(Case > 0) printf(" "); 14 now = 0; 15 memset(ans, 0, sizeof(ans)); 16 scanf("%s %s", s1, s2); 17 int len1 = strlen(s1), len2 = strlen(s2), i = len1 - 1, j = len2 - 1; 18 while(i >= 0 && j >= 0) { 19 ans[now + 1] = (ans[now] + s1[i] + s2[j] - 2 * '0') / 10; 20 ans[now] = (ans[now ++] + s1[i --] + s2[j --] - 2 * '0') % 10; 21 } 22 while(i >= 0) { 23 ans[now + 1] = (ans[now] + s1[i] - '0') / 10; 24 ans[now] = (ans[now ++] + s1[i --] - '0') % 10; 25 } 26 while(j >= 0) { 27 ans[now + 1] = (ans[now] + s2[j] - '0') / 10; 28 ans[now] = (ans[now ++] + s2[j --] - '0') % 10; 29 } 30 printf("Case %d: %s + %s = ", ++ Case, s1, s2); 31 if(ans[now] > 0) now ++; 32 for(i = now - 1; i >= 0; i --) 33 printf("%d", ans[i]); 34 printf(" "); 35 } 36 return 0; 37 }