#1001,Problem A+B(Big Integer)

10165101292 edited 7 年,7 月前

贴代码

include

include

//翻转字符串
void reverse(char *a)
{
int len = strlen(a);
for (int i = 0; i < (len / 2); i++)
{
char temp = a[len - 1 - i];
a[len - 1 - i] = a[i];
a[i] = temp;
}
}

int main()
{
char ina[501], inb[501];
while (1)
{
//读取字符串
if (scanf(“%s %s”, ina, inb) == -1)
break;

    //两字符串长度
    int lena = strlen(ina);
    int lenb = strlen(inb);

    //翻转
    reverse(ina);
    reverse(inb);

    //为短的字符串补0字符便于后续操作
    if (lena > lenb)
        for (int i = lenb; i < lena; i++)
            inb[i] = '0';
    else
        for (int i = lena; i < lenb; i++)
            ina[i] = '0';

    int max = (lena > lenb ? lena : lenb);

    //初始化存放结果的数组
    int res[502];
    for (int i = 0; i < 502; i++)
        res[i] = 0;

    //模拟计算
    for (int i = 0; i < max; i++)
    {
        int temp = ina[i] - '0' + inb[i] - '0'+res[i];
        if (temp < 10)   
            res[i] = temp;
        else {
            res[i] = temp % 10;
            res[i + 1] += temp / 10;  //溢出往上进位
        }
    }

    //检查最后是否进位
    if (res[max] != 0)
        max++;

    //打印
    for (int i = max - 1; i >= 0; i--)
        printf("%d", res[i]);

    printf("\n");
}

}

Comments

10185101151

include

include

define SIZE 500
int main()
{
char a[SIZE];
char b[SIZE];
char c[SIZE];
char d[SIZE];
int i,j,h;

while (scanf(“%s”,d)!=EOF)
{
int m,n=0;
for (i=0;i<SIZE;i++)
{
b[i]=‘0’;
a[i]=‘0’;
}
j=strlen(d);
scanf(“%s”,c);
h=strlen(c);
if(j>=h)
{
for(h–;h>=0;h–)
b[–j]=c[h];
for(j=0;j<=strlen(d)-1;j++)
a[j]=d[j];
j=strlen(d);
}
else
{

for(j--;j&gt;=0;j--)
b[--h]=d[j];
for(h=0;h&lt;=strlen(c)-1;h++)
a[h]=c[h];
j=strlen(c);

}
for(i=j-1;i>=0;i–)
{
m=a[i]+b[i]+n-2*‘0’;
n=m/10;
m=m%10;
c[i]=m+‘0’;
}
if(n>0)
printf(“%d”,n);
for(i=0;i<=j-1;i++)
if(i==j-1)
printf(“%c\n”,c[i]);
else
printf(“%c”,c[i]);
}
return 0;
}

10185101151

include

define SIZE 500
int main()
{
char a[SIZE];
char b[SIZE];
char c[SIZE];
char d[SIZE];
int i,j,h;

while (scanf(“%s”,d)!=EOF)
{
int m,n=0;
for (i=0;i<SIZE;i++)
{
b[i]=‘0’;
a[i]=‘0’;
}
j=strlen(d);
scanf(“%s”,c);
h=strlen(c);
if(j>=h)
{
for(h–;h>=0;h–)
b[–j]=c[h];
for(j=0;j<=strlen(d)-1;j++)
a[j]=d[j];
j=strlen(d);
}
else
{

for(j--;j&gt;=0;j--)
b[--h]=d[j];
for(h=0;h&lt;=strlen(c)-1;h++)
a[h]=c[h];
j=strlen(c);

}
for(i=j-1;i>=0;i–)
{
m=a[i]+b[i]+n-2*‘0’;
n=m/10;
m=m%10;
c[i]=m+‘0’;
}
if(n>0)
printf(“%d”,n);
for(i=0;i<=j-1;i++)
if(i==j-1)
printf(“%c\n”,c[i]);
else
printf(“%c”,c[i]);
}
return 0;
}

10175101215

include

include

define SIZE 500

int main()
{
char a[SIZE];
char b[SIZE];
char c[SIZE];
char d[SIZE];
int i,j,h;

while (scanf("%s",d)!=EOF)
{
int m,n=0;
for (i=0;i<SIZE;i++)
    {
        b[i]='0';
        a[i]='0';
    }
j=strlen(d);
scanf("%s",c);
h=strlen(c);
if(j>=h)
{
    for(h--;h>=0;h--)
    b[--j]=c[h];
    for(j=0;j<=strlen(d)-1;j++)
    a[j]=d[j];
    j=strlen(d);
}
else
{

    for(j--;j>=0;j--)
    b[--h]=d[j];
    for(h=0;h<=strlen(c)-1;h++)
    a[h]=c[h];
    j=strlen(c);
}
for(i=j-1;i>=0;i--)
{
    m=a[i]+b[i]+n-2*'0';
    n=m/10;
    m=m%10;
    c[i]=m+'0';
}
if(n>0)
    printf("%d",n);
for(i=0;i<=j-1;i++)
    if(i==j-1)
    printf("%c\n",c[i]);
    else
    printf("%c",c[i]);
    }
return 0;

}