杭电1002,本打算用字符串直接相加的,后来一想,换成整形数组来算也未必不可呀,只是一位一位算了也可以的。
#include <stdio.h>
#include <string.h>
void strr(char a[])
{
int i, j;
char t;
for(i = 0, j = strlen(a) -1; i < strlen(a)/2; i++, j--)
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}//杭电上没有倒置字符串的函数,自己写一个就ok了。
int main()
{
char a[1500], b[1500];
int i, t = 1, k, n,c[1500],d[1500], e[1500];
scanf("%d",&n);
while(n--)
{
scanf("%s",a);
scanf("%s",b);
strr(a);
strr(b);
memset(c,0,sizeof(c));
memset(d,0,sizeof(d));
memset(e,0,sizeof(e));
for(i = 0;i< strlen(a);i++) c[i] = a[i] - '0';
for(i = 0; i< strlen(b); i++) d[i] = b[i]-'0';
for(i = 0; i < strlen(a) || i < strlen(b); i++) e[i] = c[i] + d[i];
k= i;
for(i = 0; i< k; i++) e[i+1] += e[i] / 10,e[i] = e[i]%10 ;
if(e[i] > 0) k = i;
else k = i-1;
strr(a);
strr(b);
printf("Case %d:\n", t++);
printf("%s + %s = ",a,b);
for(i = k; i >= 0 ; i--) printf("%d",e[i]);
printf("\n");
if(n) printf("\n");
}
return 0;
}
原来提交上去一直pe, 额,后来认真看了题“Note there are some spaces int the equation. Output a blank line between two test cases”
前一句说的是注意方程之间有空格,后一句说的是两个实例之间有一空行,前一句好说,后一句还包含的意思是最后一个实例后面没有空格。