View Code
View Problem
---------------------------------------------------------------------
View Problem
---------------------------------------------------------------------
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #define MAX 300 void rev(char *from,char *to) { int len,i; len=strlen(from); for(i=0;i<len;i++) to[i]=from[len-i-1]; to[len]='\0'; } void add(char *first,char *sec,char *result) { char F[MAX],S[MAX],R[MAX]; int flen,slen,now,extra,sum; flen=strlen(first); slen=strlen(sec); rev(first,F); rev(sec,S); for(now=0,extra=0;(now<flen&&now<slen);now++) { sum=(F[now]-'0')+(S[now]-'0')+extra; R[now]=sum%10+'0'; extra=sum/10; } for(;now<flen;now++) { sum=F[now]+extra-'0'; R[now]=sum%10+'0'; extra=sum/10; } for(;now<slen;now++) { sum=S[now]+extra-'0'; R[now]=sum%10+'0'; extra=sum/10; } if(extra!=0) R[now++]=extra+'0'; R[now]='\0'; if(strlen(R)==0) strcpy(R,"0"); rev(R,result); } int main() { static char f[MAX],s[MAX],r[MAX]; int i,len,t=0; while(gets(f)!=NULL) { if(t==0) { t++; strcpy(s,f); } else if(f[0]=='0') break; else{ add(f,s,r); strcpy(s,r); } } len=strlen(r); for(i=0;i<len;i++) printf("%c",r[i]); printf("\n"); return 0; }
No comments:
Post a Comment