zoukankan      html  css  js  c++  java
  • hdu-1002 A + B Problem II

    A + B Problem II

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 355627    Accepted Submission(s): 68970


    Problem Description
    I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
     
    Input
    The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
     
    Output
    For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
     
    Sample Input
    2
    1 2 1
    12233445566778899 998877665544332211
     
    Sample Output
    Case 1: 1 + 2 = 3
    Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
     
    大数加法
    我们都知道我们最常用的两种整数类型储存范围:
    int :[-2147483648,2147483647(2^31 - 1)] 我喜欢简单的记为如果超过9位数则不适合用int;
    long long:[-9223372036854775808,9223372036854775807(2^63 - 1)]我喜欢简单地记为如果超过19位不适合用long long;
    至于为什么我只说明了这两个,是因为我只习惯用这两个=。=;
    如果Longlong都不合适,那么直接按照大数处理了
    大数加法的方法我们可以看为小学加法竖式的思想来写,先用将两个大数存入两个字符串中,然后将两个字符串进行反转,存入两个数组中,然后从最末尾开始相加,然后将和反转,顺序输出即可,例如第二个样例:
    112233445566778899和998877665544332211翻转后变成
    998877665544332211
    112233445566778899 +
    -------------------------------
    0111111111111111111
    然后将得到的结果进行翻转,输出1111111111111111110
    贴上代码:
    #include<stdio.h>
    #include<string.h>
    int main()
    {
        char s1[1010],s2[1010];
        int i,j,k=1,m,n,t;
        scanf("%d",&t);
        while(t--)
        {
            int a[1010]={0},b[1010]={0};
            scanf("%s%s",s1,s2);
            m=strlen(s1);n=strlen(s2);
            strrev(s1);strrev(s2);
            for(i=0;s1[i]!='';i++)
            a[i]=s1[i]-'0';
            for(i=0;s2[i]!='';i++)
            b[i]=s2[i]-'0';
            if(n>m)
            m=n;
            for(i=0;i<m;i++)
            {
                a[i]=a[i]+b[i];
                if(a[i]>9)
                {
                    a[i+1]++;
                    a[i]=a[i]-10;
                }
            }
            strrev(s1);strrev(s2);//太早之前写的代码,后来发现很多OJ不支持这个翻转函数,慎用。。。
            printf("Case %d:
    ",k);
            printf("%s",s1);
            printf(" + ");
            printf("%s",s2);
            printf(" = ");
            while(a[m]==0)
            m--;
            for(i=m;i>0;i--)
            printf("%d",a[i]);
            printf("%d",a[0]);
            printf("
    ");
            if(t!=0)
            printf("
    ");
            k++; 
        }
        return 0;
    }

    本来我想把这道题归到字符串里面,后来想了想,还是太鱼了~~

    曾经我做这道题的时候还是一条鱼摆摆~

  • 相关阅读:
    Linux之硬件资源管理
    Linux之vi 文本编辑命令
    [2]窗口程序的创建
    [1]cs辅助项目分解
    1.3 Windows操作系统
    (PYG)学习去除软件自效验
    CSUST 4006-你真的会树套树套树吗?(贪心|dp)
    CSUST 4003-你真的会泡面吗?(优先队列模拟)
    2020牛客暑期多校第八场I-Interesting Computer Game(离散化+并查集)
    2020牛客暑期多校K-Kabaleo Lite(贪心)
  • 原文地址:https://www.cnblogs.com/love-sherry/p/6745021.html
Copyright © 2011-2022 走看看