大数加法

1.

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;

int main()
{
    char a[201],b[201];
    int c[201],d[201];
    int l1,l2,l,t;
    int flag;
    while(scanf("%s",a)!=EOF)
    {
        flag=0;
        scanf("%s",b);
        l1=strlen(a);
        l2=strlen(b);
        memset(c,0,sizeof(c));
        memset(d,0,sizeof(d));
        if(l1>=l2)
            l=l1;
        else l=l2;
        for(int i=0;i<l;i++)
        {
           if(l1-1>=0)
           {
               c[i]=a[l1-1]-'0';
               l1--;
           }
           else c[i]=0;
           if(l2-1>=0)
           {
               d[i]=b[l2-1]-'0';
               l2--;
           }
           else d[i]=0;
        }
        for(int i=0;i<l;i++)
        {
            t=c[i]+d[i];
            if(t>9)
            {
                c[i]=t-10;
                c[i+1]++;
            }
            else c[i]=t;
        }
        /*if(!c[l])
        {
            l++;
           for(int i=l-1;i>=0;i--)
           {
            printf("%d",c[i]);
           }
           printf("\n");
        }*/
        for(int i=l;i>=0;i--)
        {
            if(flag||c[i])
            {
                flag=1;
                printf("%d",c[i]);
            }
         }
            if(!flag) printf("0");
            printf("\n");

    }
    return 0;
}

2.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

int main()
{
    int T,t,p=0;
    char a[1001],b[1001],q[1001],w[1001];
    int l1,l2,l;
    int c[1001],d[1001];
    scanf("%d",&T);
    while(T--)
    {
        p++;
        memset(c,0,sizeof(c));
        memset(d,0,sizeof(d));
        scanf("%s%s",a,b);
        strcpy(q,a);
        strcpy(w,b);
        l1=strlen(a);
        l2=strlen(b);
        if(l1>=l2)
        {
            l=l1;
        }
        else l=l2;
        for(int i=0;i<l;i++)
        {
            if(l1-1>=0)
            {
                c[i]=a[l1-1]-'0';
                l1--;
            }
            else c[i]=0;
            if(l2-1>=0)
            {
                d[i]=b[l2-1]-'0';
                l2--;
            }
            else d[i]=0;
        }
        for(int i=0;i<l;i++)
        {
            t=c[i]+d[i];
            if(t>9)
            {
                c[i]=t%10;
                c[i+1]=t/10+c[i+1];

            }
            else c[i]=t;
        }
       printf("Case %d:\n",p);
       printf("%s + %s = ",q,w);
       int flag=0;
       for(int i=l;i>=0;i--)
       {
           if(flag||c[i])
           {
               printf("%d",c[i]);
               flag=1;
           }
       }
       printf("\n");
       //if(p!=T)
       // printf("\n");

    }
    return 0;
}
3.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

int main()
{
    int T;
    char a[1001];
    int b[1001];
    scanf("%d",&T);
    getchar();
    int l1;
    while(T--)
    {
        memset(b,0,sizeof(b));
        while(scanf("%s",a)!=EOF)
        {
          if(a[0]=='0') break;
          l1=strlen(a);
          int j=0;
          for(int i=l1-1;i>=0;i--)
          {
             b[j]=b[j]+a[i]-'0';
             j++;
          }
        }
        int t;
        for(int i=0;i<1001;i++)
        {
            t=b[i];
            if(b[i]>9)
            {
                b[i]=t%10;
                b[i+1]=b[i+1]+t/10;
            }
            else b[i]=t;
        }
        int flag=0;
        for(int i=1001-1;i>=0;i--)
        {
             if (flag || b[i])
             {
               flag = 1;
               printf("%d",b[i]);
             }
       }
       if (!flag) printf("0");
       printf("\n");
       if(T>0) printf("\n");

    }
    return 0;
}

4.没A

#include <cstdio>
#include <cstring>
#include <iostream>
#define maxn 421
using namespace std;

int main()
{
    char a[401],b[401];
    int c[401],d[401],e[401],f[401];
    int l1,l2,flag,flag1,k,k1;
    while(scanf("%s%s",a,b)!=EOF)
    {
        flag=0;
        flag1=0;
        l1=strlen(a);
        memset(c,0,sizeof(c));
        memset(d,0,sizeof(d));
        memset(e,0,sizeof(e));
        memset(f,0,sizeof(f));
        for(int i=0;i<l1;i++)
        {
            if(a[i]=='.')
            {
                flag=1;
                k=i;
                break;
            }
            else k=l1;
        }
         l2=strlen(b);
        for(int i=0;i<l2;i++)
        {
            if(b[i]=='.')
            {
                flag1=1;
                k1=i;
                break;
            }
            else k1=l2;
        }
        int max;
        max=l1-k-1;
        if(max<l2-1-k1)
            max=l2-k1;
        int j=0;
        for(int i=k-1;i>=0;i--)
        {
            c[j]=a[i]-'0';
            j++;
        }
        j=0;
        for(int i=k1-1;i>=0;i--)
        {
            d[j]=b[i]-'0';
            j++;
        }
        int t;
        for(int i=0;i<401;i++)
        {
            t=c[i]+d[i];
            if(t>9)
            {
                c[i]=t%10;
                c[i+1]=c[i+1]+t/10;
            }
            else c[i]=t;
        }
        j=0;
        for(int i=l1-1;i>=k+1;i--)
        {
            e[j]=a[i]-'0';
            j++;
        }
        j=0;
        for(int i=l2-1;i>=k1+1;i--)
        {
            f[j]=b[i]-'0';
            j++;
        }
        for(int i=0;i<max;i++)
        {
            t=e[i]+f[i];
            if(t>9)
            {
                if(i+1==max)
                {
                   e[i]=t%10;
                   c[0]=c[0]+t/10;
                }
                else
                {
                e[i]=t%10;
                e[i+1]=e[i+1]+t/10;
                }
            }
            else e[i]=t;
        }
        int r=0;
        for(int i=400-1;i>=0;i--)
        {
            if(r||c[i])
            {
                r=1;
                printf("%d",c[i]);
            }
        }
        r=0;
        if(flag||flag1)
        {
            for(int i=399;i>=0;i--)
            if(r||e[i])
            {
                r=1;
                break;
            }

        }
        if(r==1)
        {
           printf(".");
           r=0;
           for(int i=400-1;i>=0;i--)
          {
            if(r||e[i])
            {
                r=1;
                printf("%d",e[i]);
            }
           }
        }
        printf("\n");

    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值