题目本身属于简单题,但是注意在输出的时候,题目很变态的对格式做了很多要求:
1.输入的N与下面的block有一个空行
2.每次输出与下一个输入的block有一个空行
3.但是特别注意,当是最后一个输出时不需要输出空行
见代码:
#include <stdio.h> #include <string.h> void invert(char *a) { int l=strlen(a); for(int i=0,j=l-1;i<j;i++,j--) { char t; t=a[i]; a[i]=a[j]; a[j]=t; } } char * sum(char *a,char *b) //length of a is biger than b { int la=strlen(a),lb=strlen(b); int c=0,i; for(i=0;i<lb;i++) { int t=a[i]+b[i]-'0'-'0'+c; if(t/10!=0) { c=t/10; t=t%10; a[i]=t+'0'; } else { a[i]=t+'0'; c=0; } } if(c!=0) { for(i=lb;i<la;i++) { int t=a[i]+c-'0'; if(t/10!=0) { c=t/10; t=t%10; a[i]=t+'0'; } else { a[i]=t+'0'; c=0; break; } } if(c!=0) { a[i++]=c+'0'; a[i]='\0'; } } return a; } int main() { int N; scanf("%d",&N); while(N--) { getchar(); char t[110],result[110]; int i=0; scanf("%s",t); if(!strcmp(t,"0")) { printf("0\n"); continue; } else { invert(t); strcpy(result,t); } while(scanf("%s",t)!=EOF&&strcmp(t,"0")) { int lr=strlen(result),lt=strlen(t); invert(t); if(lr>=lt) strcpy(result,sum(result,t)); else strcpy(result,sum(t,result)); } invert(result); printf("%s\n",result); if(N>0) printf("\n"); } return 0; }