标签:des style blog class code java
Time
Limit: 2000/1000 MS (Java/Others) Memory Limit:
65536/32768 K (Java/Others)
Total Submission(s):
11678 Accepted Submission(s):
2936
仅仅为了存一个高精度大数相加的模板
1 #include <iostream> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <algorithm> 5 #include <cstring> 6 #include <math.h> 7 #include <ctime> 8 using namespace std; 9 #define maxn 32768 10 void add(char a[],char b[],char back[]) 11 { 12 int i,j,k,up,x,y,z,l; 13 char *c; 14 if (strlen(a)>strlen(b)) l=strlen(a)+2; else l=strlen(b)+2; 15 c=(char *) malloc(l*sizeof(char)); 16 i=strlen(a)-1; 17 j=strlen(b)-1; 18 k=0;up=0; 19 while(i>=0||j>=0) 20 { 21 if(i<0) x=‘0‘; else x=a[i]; 22 if(j<0) y=‘0‘; else y=b[j]; 23 z=x-‘0‘+y-‘0‘; 24 if(up) z+=1; 25 if(z>9) {up=1;z%=10;} else up=0; 26 c[k++]=z+‘0‘; 27 i--;j--; 28 } 29 if(up) c[k++]=‘1‘; 30 i=0; 31 c[k]=‘\0‘; 32 for(k-=1;k>=0;k--) 33 back[i++]=c[k]; 34 back[i]=‘\0‘; 35 } 36 37 int main() 38 { 39 int n; 40 scanf("%d",&n); 41 char a[maxn]="0"; 42 while(n--) 43 { 44 char b[maxn],c[maxn]; 45 memset(b,‘\0‘,sizeof(b)); 46 memset(c,‘\0‘,sizeof(c)); 47 memset(a,‘\0‘,sizeof(a)); 48 a[0]=‘0‘; 49 while(1) 50 { 51 scanf("%s",b); 52 if(b[0]==‘0‘) 53 break; 54 add(a,b,c); 55 strcpy(a,c); 56 memset(b,‘\0‘,sizeof(b)); 57 memset(c,‘\0‘,sizeof(c)); 58 } 59 if(n==0) 60 printf("%s\n",a); 61 else 62 printf("%s\n\n",a); 63 64 } 65 return 0; 66 }
hdu acm-1047 Integer Inquiry(大数相加),布布扣,bubuko.com
hdu acm-1047 Integer Inquiry(大数相加)
标签:des style blog class code java
原文地址:http://www.cnblogs.com/vivider/p/3704400.html