大数相加(代码错误,求改正)
发布时间:2020-12-14 04:09:29 所属栏目:大数据 来源:网络整理
导读:1、http://acm.hdu.edu.cn/showproblem.php?pid=1753 2、题目: 大明A+B Time Limit: 3000/1000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5959????Accepted Submission(s): 2045 Problem Description 话说,经
|
1、http://acm.hdu.edu.cn/showproblem.php?pid=1753 2、题目:
大明A+BTime Limit: 3000/1000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5959????Accepted Submission(s): 2045
Problem Description
话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫“大明”。
这时他已经不是那个只会做100以内加法的那个“小明”了,现在他甚至会任意长度的正小数的加法。 现在,给你两个正的小数A和B,你的任务是代表大明计算出A+B的值。
?
Input
本题目包含多组测试数据,请处理到文件结束。
每一组测试数据在一行里面包含两个长度不大于400的正小数A和B。
?
Output
请在一行里面输出输出A+B的值,请输出最简形式。详细要求请见Sample Output。
?
Sample Input
?
Sample Output
?
Author
linle
?
Source
2007省赛集训队练习赛(6)_linle专场
3、代码:(wrong) #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char a[1000];
char b[1000];
char c[1000];
int main()
{
while(scanf("%s%s",a,b)!=EOF)
{
int la=strlen(a);
int lb=strlen(b);
int flag1=0,flag2=0;
for(int i=0; i<la; i++)
{
if((a[i]!='0')&&(a[i]!='.'))
flag1=1;
}
for(int i=0; i<lb; i++)
{
if((b[i]!='0')&&(b[i]!='.'))
flag2=1;
}
if(0==flag1&&0==flag2)
printf("0n");
else
{
int posa=la;
int posb=lb;
for(int i=0; i<la; i++)
{
if(a[i]=='.')
{
posa=i;
break;
}
}
for(int i=0; i<lb; i++)
{
if(b[i]=='.')
{
posb=i;
break;
}
}
int maxs=max(posa,posb);
int maxe=max(la-posa,lb-posb);
int l=maxs+maxe;
char tempa[1000];
char tempb[1000];
if(posa<maxs)
{
int pp=maxs-posa;
int ppp=pp;
for(int i=0; i<pp; i++)
tempa[i]='0';
for(int i=0; i<la; i++)
{
tempa[pp++]=a[i];
}
for(int i=pp; i<l; i++)
tempa[i]='0';
tempa[l]=' ';
tempa[posa+ppp]='.';
}
else
{
strcpy(tempa,a);
for(int i=la; i<l; i++)
tempa[i]='0';
tempa[l]=' ';
tempa[posa]='.';
}
if(posb<maxs)
{
int pp=maxs-posb;
int ppp=pp;
//printf("&&&%d %d %dn",maxs,posb,pp);
for(int i=0; i<pp; i++)
tempb[i]='0';
for(int i=0; i<lb; i++)
{
tempb[pp++]=b[i];
}
for(int i=pp; i<l; i++)
tempb[i]='0';
tempb[l]=' ';
tempb[posb+ppp]='.';
//printf("**%d %d %d %d %sn",posa,pp,tempb);
}
else
{
strcpy(tempb,b);
for(int i=lb; i<l; i++)
tempb[i]='0';
tempb[l]=' ';
tempb[posb]='.';
}
//printf("%sn%sn",tempa,tempb);
//printf("%d %cn",strlen(tempa),tempa[l-1]);
int temp=0;
for(int i=l-1; i>=0; i--)
{
if(tempa[i]!='.')
{
int ans=(tempa[i]-'0')+(tempb[i]-'0')+temp;
if(ans>9)
{
c[i]=ans-10+'0';
temp=1;
}
else
{
c[i]=ans+'0';
temp=0;
}
}
else
c[i]='.';
}
c[l]=' ';
for(int i=l-1; i>=0; i--)
{
if((c[i]!='0'))
{
if(c[i]=='.')
{
c[i]=0;c[i+1]=' ';
break;
}
break;
}
}//printf("**%sn",c);
int tmp=0;
for(int i=0;i<l;i++)
{
if((c[i]!='0'))
{
tmp=i;
break;
}
}
if(1==temp)
{
printf("1%sn",c);
}
else if(c[tmp]=='.')//tmp>=2)
{
printf("0%sn",c+tmp);//if((c[tmp-1]=='.')&&(c[tmp-2]=='0'))
//tmp-=2;
}
//if(temp==1)
// printf("1%sn",c+tmp);
//else
// printf("%sn",c+tmp);
else
printf("%sn",c+tmp);
}
}
return 0;
}
/*
1.1 2.9
1.2222222222 2.3444323343
1 1.1
1.2345
12.45
*/
/*
0 0
1.2333 1.2667
99999 1
12121213213124342545354545 13312312321312321321.02300000000000000000000000000
*/
/*
00.1 1
*/
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
