朱色虫居
Pages
Home
Featured Posts
2006/09/23
424-Integer Inquiry
/*
"Integer Inquiry"
Level:4.5
Date:2006/9/22
大數加法,
技巧:先把計算結果array反轉,在array中是由LSB to MSB排列.
進位要考慮到多重進位.
*/
#include
#include
main(){
int ans[102]={0};
char input[101]={0}; //包括'\0'.
int i=0,len=0,carry=0,tmp=0;
bool skip = true;
while(1){
scanf("%s",&input);
len = strlen(input);
if(*input == '0' && len ==1)
break;
for(i=0;i
= 10){
carry = tmp/10;
tmp %=10;
}
ans[i] = tmp;
if(i == len-1 && carry > 0){
for(i=len-1;carry>0;i++){ //handle multiple carry out
ans[i+1]+=carry;
carry = ans[i+1]/10;
ans[i+1]%=10;
}
}
}
}
for(i=101;i>=0;i--){
if(ans[i]==0 && skip == true)
continue;
skip = false;
printf("%d",ans[i]);
}
printf("\n");
}
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment