标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16773 Accepted Submission(s): 4326
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 char sa[1000],sb[1000]; 6 int a[1000],b[1000],c[1000]; 7 void add(char sa[1000],char sb[1000]){ 8 int lena=strlen(sa); 9 int lenb=strlen(sb); 10 int lenc=(lena>lenb?lena:lenb); 11 memset(a,0,sizeof(a)); 12 memset(b,0,sizeof(b)); 13 14 for(int j=0;j<lena;j++){ 15 a[lena-1-j]=sa[j]-‘0‘; 16 } 17 for(int j=0;j<lenb;j++){ 18 b[lenb-1-j]=sb[j]-‘0‘; 19 } 20 memset(c,0,sizeof(c)); 21 for (int ii=0; ii<lenc; ii++) { 22 c[ii]=a[ii]+b[ii]+c[ii]; 23 if (c[ii]>=10) { 24 c[ii+1]=1; 25 c[ii]-=10; 26 } 27 } 28 if (c[lenc]>0) lenc++; 29 30 for (int ii=lenc-1; ii>=0; ii--){ 31 sb[ii]=c[lenc-1-ii]+‘0‘; 32 } 33 sb[lenc]=‘\0‘; 34 35 } 36 int main() { 37 int n,l1,l2,k,maxn=0; 38 scanf("%d",&n); 39 while(n--) { 40 k=0; 41 strcpy(sb,"0"); 42 while(scanf("%s",sa),strcmp(sa,"0")) { 43 k++; 44 add(sa,sb); 45 } 46 puts(sb); 47 //putchar(‘\n‘); 48 49 if(n) 50 putchar(‘\n‘); 51 } 52 return 0; 53 }
标签:
原文地址:http://www.cnblogs.com/livelihao/p/5155483.html