用数组来算大整数的加法

今天我写了一个代码,我同学写了一个代码,都是关于用数组算大整数加法的问题。我们都用了一个数组字符储存四位的方法来算的。

</pre><pre name="code" class="cpp">#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
#include<string.h>
#include<math.h>
int main()
{

    char b[1000],a[1000],c[10];
    int d[100],e[100],f[100] ,i, k, len1,len2,sum;
    gets(a);
    gets(b);
    strrev(a);
    strrev(b);

    memset(d,0,sizeof(d));
    memset(e,0,sizeof(e));
    memset(f,0,sizeof(f));
    for(i = 0, k= 0;  i   < strlen(a); i+=4)
    {
        c[0] = a[i], c[1] =a[i+1],c[2] = a[i+2],c[3] = a[i+3],c[4] = '\0';
        strrev(c);
        d[k++] = atoi(c);
    }
    len1 = k;
    for(i = 0, k = 0;  i  < strlen(b); i+=4)
    {
         c[0] = a[i], c[1] =a[i+1],c[2] = a[i+2],c[3] = a[i+3],c[4] = '\0';
          strrev(c);
        e[k++] = atoi(c);
    }
    len2 = k;
    for(i = 0, k = 0; i< len1|| i < len2; i++)
    {
        sum = log10(d[i] + e[i])+1;
        if(sum > 4) d[i+1] ++, f[k++] = d[i] + e[i] -10000 , f[k]++;
        else f[k++] =d[i] + e[i];
    }
    for(i = k-1; i>= 0; i--)  printf("%d",f[i]);
    printf("\n");

    return 0;
}
这是我的代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值