C语言大数加法程序!这个怎么无法输出结果的,菜鸡求救啊?
#include
#include
int main()
{char a[100],b[100],c[100];
int i,t,l1,l2,l3;
gets(a);
gets(b);
//反序
l1=strlen(a);
for(i=0;i<=l1/2;i++)
{
t=a[i]-'0';
a[i]=a[l1-i]-'0';
a[i-i]=t;
}
l2=strlen(b);
for(i=0;i<=l2/2;i++)
{
t=b[i]-'0';
b[i]=b[l2-i]-'0';
b[i-i]=t;
}
l3=l1>l2?l1:l2;
c[1]=a[1]+b[1];
for(i=1;i<=l3;i++)
{
if(c[i-1]/10==1)
{
c[i-1]%=10;
c[i]=(a[i]+b[i])%10+1;
}
else
c[i]=(a[i]+b[i]);
}
if(c[i-1]/10==1)
{
c[i-1]%=10;
c[i]=1;
c[i+1]='\0';
}
else
c[i]='\0';
l3=strlen(c);
for(i=l3;i>=0;i--)
printf("%c",c[i]);
}
分享到:
更多
------解决方案--------------------
仅供参考#include
#include
#define MAXLEN 1000
char a1[MAXLEN];
char a2[MAXLEN];
static int v1[MAXLEN];
static int v2[MAXLEN];
static int v3[MAXLEN];
int i,j,n,L,z;
void main(void) {
scanf("%d",&n);
for (j=0;j
scanf("%s%s",a1,a2);
L=strlen(a1);
for (i=0;i
L=strlen(a2);
for (i=0;i
for (i=0;i
for (i=0;i
if (v3[i]>=10) {
v3[i+1]+=v3[i]/10;
v3[i]=v3[i]%10;
}
}
printf("Case %d:\n", j+1);
printf("%s + %s = ", a1, a2);
z=0;
for (i=MAXLEN-1;i>=0;i--) {
if (z==0) {
if (v3[i]!=0) {
printf("%d",v3[i]);
z=1;
}
} else {
printf("%d",v3[i]);
}
}
if (z==0) printf("0");
printf("\n");
}
}
//Sample Input
//3
//0 0
//1 2
//112233445566778899 998877665544332211
//
//Sample Output
//Case 1:
//0 + 0 = 0
//Case 2:
//1 + 2 = 3
//Case 3:
//112233445566778899 + 998877665544332211 = 1111111111111111110
------解决方案--------------------
引用:Quote: 引用:仅供参考#include
#include
#define MAXLEN 1000
char a1[MAXLEN];
char a2[MAXLEN];
static int v1[MAXLEN];