zoukankan      html  css  js  c++  java
  • HDU 1002 A + B Problem II (大数加法)

    题目链接

    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
    112233445566778899 998877665544332211
    

    Sample Output

    Case 1:
    1 + 2 = 3
    
    Case 2:
    112233445566778899 + 998877665544332211 = 1111111111111111110
    

    分析:

    大数问题肯定是当作字符串来处理的,首先要把数的每个位数逆序存到一个int型的数组中,然后将这两个数组的对应位置上的数相加,如果有进位的话还应加上进位,最后输出的时候应该先找到首个不为0 的数,然后将后面的数输出来。

    代码:

    #include<iostream>
    #include<math.h>
    #include<stdio.h>
    #include<string.h>
    using namespace std;
    int main()
    {
        int n;
        scanf("%d",&n);
        getchar();
        for(int h=1; h<=n; h++)
        {
            char a[10000],b[10000];
            int c[10000],d[10000];
            int e[10000];
            int k,l;
            memset(c,0,sizeof(c));
            memset(d,0,sizeof(d));
          scanf(" %s %s",a,b);
            k=strlen(a);
            l=strlen(b);
            for(int i=0; i<k; i++)
                c[i]=a[k-i-1]-'0';
            for(int i=0; i<l; i++)
                d[i]=b[l-i-1]-'0';
            int op=0;
            for(int i=0; i<1000; i++)
            {
                e[i]=(c[i])+(d[i])+op;
                if(e[i]>=10)
                {
                    e[i]=e[i]%10;
                    op=1;
                }
                else op=0;
            }
            printf("Case %d:
    ",h);
            printf("%s + %s = ",a,b);
            int i;
            for( i=999; i>=0; i--)
                if(e[i]!=0)break;
            for(int j=i; j>=0; j--)
                printf("%d",e[j]);
            printf("
    ");
            if(h!=n)
                printf("
    ");
            memset(a,0,sizeof(a));
            memset(b,0,sizeof(b));
        }
        return 0;
    }
  • 相关阅读:
    序列化器:serializers(django-rest-framework)
    数据库模型:models(Django)
    AtCoder Beginner Contest 213【A
    Codeforces Round #736 (Div. 2)【ABCD】
    AtCoder Beginner Contest 212【A
    Codeforces Round #732 (Div. 2)【ABCD】
    VS201X windows下编译提示缺少ucrtbased.dll文件
    Locust1.6 从入门到实战
    如何理解Windows认证流程
    HTB::Forest
  • 原文地址:https://www.cnblogs.com/cmmdc/p/6767241.html
Copyright © 2011-2022 走看看