HDU 1047 || ZOJ 1292 大数相加
2012-08-20 09:47
330 查看
同样的代码在zoj AC了,但是hoj里不是WA就是TLE。。。
让同学修改了一下,发现少考虑了一种情况。。。
例:
1
000
00
0
输出: 0
#include<stdio.h>
#include<string.h>
#define L 110
int main(){
char s[L];
int N,i,j,len,f,sum[L];
scanf("%d",&N);
while(N--){
memset(sum,0,sizeof(sum));
while(scanf("%s",s)!=EOF){
if(strcmp(s,"0")==0)break;
len=strlen(s);
f=0;
for(i=len-1,j=L-1;j>=0;i--,j--){
if(i>=0) {
sum[j]+=(s[i]-'0'+f);
if(sum[j]>=10){
sum[j]-=10;
f=1;
}
else f=0;
}
else {
sum[j]+=f;
if(sum[j]>=10){
sum[j]-=10;
f=1;
}
else f=0;
}
}
}
for(j=0;sum[j]==0;j++);
if(j<=L-1)
for(;j<L;j++)
printf("%d",sum[j]);
else printf("0");
if(N>=1) printf("\n\n");
else printf("\n");
}
return 0;
}
让同学修改了一下,发现少考虑了一种情况。。。
例:
1
000
00
0
输出: 0
#include<stdio.h>
#include<string.h>
#define L 110
int main(){
char s[L];
int N,i,j,len,f,sum[L];
scanf("%d",&N);
while(N--){
memset(sum,0,sizeof(sum));
while(scanf("%s",s)!=EOF){
if(strcmp(s,"0")==0)break;
len=strlen(s);
f=0;
for(i=len-1,j=L-1;j>=0;i--,j--){
if(i>=0) {
sum[j]+=(s[i]-'0'+f);
if(sum[j]>=10){
sum[j]-=10;
f=1;
}
else f=0;
}
else {
sum[j]+=f;
if(sum[j]>=10){
sum[j]-=10;
f=1;
}
else f=0;
}
}
}
for(j=0;sum[j]==0;j++);
if(j<=L-1)
for(;j<L;j++)
printf("%d",sum[j]);
else printf("0");
if(N>=1) printf("\n\n");
else printf("\n");
}
return 0;
}
相关文章推荐
- HDU 1047 Integer Inquiry 大数相加 string解法
- ZOJ 1292/HDU 1047 Integer Inquiry
- zoj_1292 Integer Inquiry(大数相加)
- HDU 1047。多个大数相加
- ZOJ 1292 大数相加
- HDU 1047 Integer Inquiry (多个大数相加)
- HDU 1047 大数相加
- HDU 1047 Integer Inquiry 多个大数相加
- ZOJ 1292 多个大数相加
- HDU 1047 多个大数相加 Integer Inquiry
- HDU 1047(大数相加)
- HDU 1047(大数相加)(数组模拟)
- poj 1503 && hdu 1047 大数相加
- HDU-1047 Integer Inquiry 大数相加
- HDU 1047 Integer Inquiry 大数相加 string解法
- HDU 1047 大数相加
- HDU1002 大数相加
- [ACM]hdu 1002 A + B Problem II (复习大数相加)
- hdu 2100 大数相加
- HDU - 1047 - Integer Inquiry (大数高精度)