| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛

已结贴   问题点数：20  回复次数：1
HDU-1002 大数加法 C语言实现 一直AC不了 求助贴
A + B问题II

2
1 2
112233445566778899 998877665544332211

1 + 2 = 3

112233445566778899 + 998877665544332211 = 1111111111111111110

Sample Output
Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

#include <stdio.h>
#include <string.h>
int mark;
{
int len1,len2;
int i,j,L=0;
int temp;
len1=strlen(a);
len2=strlen(b);
for(i=0;i<len1/2;i++)
{
temp =  a[i];
a[i] = a[len1-i-1];
a[len1-i-1] = temp;
}
for(i=0;i<len2/2;i++)
{
temp =  b[i];
b[i] = b[len2-i-1];
b[len2-i-1] = temp;
}
for(i=0;i<len1&&i<len2;i++)
{
j = (a[i]-'0'+b[i]-'0'+L)%10;
L = (a[i]-'0'+b[i]-'0'+L)/10;
c[i] = j+'0';
}
if(i<len1)
{
for(;i<len1;i++)
{
j = (a[i]-'0'+L)%10;
L = (a[i]-'0'+L)/10;
c[i] = j+'0';
}
}
if(i<len2)
{
for(;i<len2;i++)
{
j = (b[i]-'0'+L)%10;
L = (b[i]-'0'+L)/10;
c[i] = j+'0';
}
}
while(L)
{

c[i++] =(L%10+'0');
L/=10;
}
mark = i-1;
}
int main()
{
int k;
//int i;
char a[1001],b[1001],c[1001];
int n;
scanf("%d",&n);
int m;
if(n>=2){
for(m=0;m<n-1;m++)
{
scanf("%s%s",a,b);
printf("Case %d:\n",m+1);
printf("%s + %s = ",a,b);
for(k=mark;k>=0;k--)
{
printf("%c",c[k]);
}
printf("\n\n");
}
while(scanf("%s%s",a,b)!=EOF)
{
printf("Case %d:\n",n);
printf("%s + %s = ",a,b);
for(k=mark;k>=0;k--)
{
printf("%c",c[k]);
}
printf("\n");
}
}
else
{
while(scanf("%s%s",a,b)!=EOF)
{
printf("Case 1:\n");
printf("%s + %s = ",a,b);
for(k=mark;k>=0;k--)
{
printf("%c",c[k]);
}
printf("\n");
}
}
return 0;
}

[此贴子已经被作者于2018-8-9 23:58编辑过]

得分:20

126邮箱联系方式：no1xijin@126. com
• 2
• 1/1页
• 1