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 |
稳妥AC,输入数据前导有0,还是用strcmp吧#include<stdio.h> #include<string.h> const int max=101; struct bigInteger{ int digit[max]; int size; void init(){ for(int i=0;i<max;i++){ digit[i]=0; } size=0; } //trans char to bigInteger void set(char str[]){ init(); int len=strlen(str); //j为了统计个数,t临时,c为了进位 int j=0,t=0,c=1; for(int i=len-1;i>=0;i--){ t+=(str[i]-'0')*c; j++; c*=10; if(j==4||i==0){ digit[size++]=t; j=0; t=0; c=1; } } } void output(){ for(int i=size-1;i>=0;i--){ if(i!=size-1){ printf("%04d",digit[i]); }else{ printf("%d",digit[i]); } } printf("\n"); } bigInteger operator + (const bigInteger &A) const{ int carry=0; bigInteger res; res.init(); for(int i=0;i<A.size||i<size;i++){ int tmp=A.digit[i]+digit[i]+carry; carry=tmp/10000; tmp%=10000; res.digit[res.size++]=tmp; } if(carry!=0){ res.digit[res.size++]=carry; } return res; } }a,b,c; char str[max]; int main() { a.set("0"); while(scanf("%s",str)!=EOF&&strcmp(str,"0")!=0){ b.set(str); a=a+b; } a.output(); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator