HDU-1002.大数相加(字符串模拟)

  本题大意:给出两个1000位以内的大数a 和b,让你计算a + b的值。

  本题思路:字符串模拟就能过,会Java的大佬应该不会点进来......

  参考代码:

 #include <cstdio>
#include <cstring>
using namespace std; const int maxn = + ;
int t, Case = , now;
int ans[maxn];
char s1[maxn], s2[maxn]; int main () {
scanf("%d", &t);
while(t --) {
if(Case > ) printf("\n");
now = ;
memset(ans, , sizeof(ans));
scanf("%s %s", s1, s2);
int len1 = strlen(s1), len2 = strlen(s2), i = len1 - , j = len2 - ;
while(i >= && j >= ) {
ans[now + ] = (ans[now] + s1[i] + s2[j] - * '') / ;
ans[now] = (ans[now ++] + s1[i --] + s2[j --] - * '') % ;
}
while(i >= ) {
ans[now + ] = (ans[now] + s1[i] - '') / ;
ans[now] = (ans[now ++] + s1[i --] - '') % ;
}
while(j >= ) {
ans[now + ] = (ans[now] + s2[j] - '') / ;
ans[now] = (ans[now ++] + s2[j --] - '') % ;
}
printf("Case %d:\n%s + %s = ", ++ Case, s1, s2);
if(ans[now] > ) now ++;
for(i = now - ; i >= ; i --)
printf("%d", ans[i]);
printf("\n");
}
return ;
}
上一篇:redis(9)集群搭建


下一篇:ubuntu Nvidia driver install