程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> HDU 1002 A + B Problem II(兩個大數相加)

HDU 1002 A + B Problem II(兩個大數相加)

編輯:關於C++

 

 

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

 

分析:對於此題做法有兩種:其一,使2字符串的中的字符數字減去'0',逐個相加大於等於10的可以使本位減10,下一位自增1,後面的處理就非常簡單了;其二,便是讀入字符串後先讓各個字符減'0',一一對應存入整形數組中;之後再相加。對於2種方法大致是相同的,都要從後面向前加,逢十進位,以及數組初始化均要初始為0,一邊方便運算。

 

#include 
#include 
#include 

char a[1001]; //開辟兩個字符數組a、b,作為兩個輸入的大數
char b[1001];
char c[1002];

int main(void)
{
    int carry = 0, n, j;
    int lena, lenb, i, lenc;

    scanf(%d, &n);
    for(j = 1; j <= n; j++)
    {
        memset(a, 0, 1001);
        memset(b, 0, 1001);
        memset(c, 0, 1002);
        scanf(%s, a);
        scanf(%s, b);
        lena = strlen(a);
        lenb = strlen(b);

        for(lena--, lenb--, i = 0, carry = 0; (lena >= 0) && (lenb >= 0); lena--, lenb--, i++)
        {
            c[i] = a[lena]-'0' + b[lenb]-'0' + carry;
            if((int)c[i] > 9)
            {
                c[i] = c[i] - 10 + '0';
                carry = 1;
            }
            else
            {
                c[i] += '0';
                carry = 0;
            }
        }

        while(lena >= 0)
        {
            c[i] = c[i] + a[lena] + carry; //有可能加上carry後還可以向前進位
            if(c[i] > '9')
            {
                c[i] -= 10;
                carry = 1;
            }
            else
                carry = 0;
            i++;
            lena--;
        }
        while(lenb >= 0)
        {
            c[i] = c[i] + b[lenb] + carry;
            if(c[i] > '9')
            {
                c[i] -= 10;
                carry = 1;
            }
            else
                carry = 0;
            i++;
            lenb--;
        }

        lenc = strlen(c);
        printf(Case %d:
, j);
        printf(%s + %s = , a, b);
        for(i = lenc-1; i >= 0; i--) //c數組中c[0]存放的是大數的最低位,c[lenc-1]存放的是大數的最高位
            printf(%c, c[i]);
        printf(
);
        if(j != n)
            printf(
);
    }

    return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved