Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
ACIn Reply To:Why is wrong answer? Posted by:chj8081 at 2008-02-02 17:41:54 #include<stdio.h> #include<stdlib.h> #include<string.h> void move(char ans[],char str[]) { int len1,len2; len1=strlen(ans); len2=strlen(str); if(len1>len2) { for(;len2>=0;len2--,len1--) str[len1]=str[len2]; for(int i=0;i<=len1;i++) str[i]='0'; } else if(len1<len2) { for(;len1>=0;len1--,len2--) ans[len2]=ans[len1]; for(int i=0;i<=len2;i++) ans[i]='0'; } } void add(char ans[],char str[]) { move(ans,str); // printf("%d %d\n",strlen(ans),strlen(str)); // puts(ans); // puts(str); int i; int flag=0; for(i=strlen(ans)-1;i>=0;i--) { int sum=flag+ans[i]-'0'+str[i]-'0'; flag=sum/10; sum=sum%10; ans[i]='0'+sum; } // printf("%s\n",ans); if(flag) { for(int j=strlen(ans);j>=0;j--) { // printf("%d\n",j); ans[j+1]=ans[j]; } ans[0]=flag+'0'; } } int main(void) { char str[103]; char ans[103]; scanf("%s",str); strcpy(ans,str); if(!strcmp(str,"0")) return 0; while(1) { // puts(str); scanf("%s",str); if(!strcmp(str,"0")) break; add(ans,str); } int i; for(i=0;ans[i]=='0';i++); printf("%s\n",ans+i); // system("PAUSE"); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator