1. 程式人生 > >HDU1002 A + B Problem II 解題報告

HDU1002 A + B Problem II 解題報告

目錄

https://blog.csdn.net/velscode/article/details/84348774

題目資訊

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.

我有一個非常簡單的任務給你,給出兩個整數A和B,你的工作是計算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.

第一行輸入包括一個整數T,1<=T<=20,是測試用例的數量。接下來的 T 行,每行包括兩個正整數A和B。注意這個整數非常大,意味著你不能用32位整型來處理他們。你可以任務每個整數的長度不會超過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.

對於每個測試用例,你應該輸出兩行。第一行是Case #:,#代表測試用例的序號。第二行是一個等式"A + B = Sum",Sum是A+B的結果。注意每個等式中有一些空格,每兩個結果中輸出一個空白行

Sample Input

2
1 2
112233445566778899 998877665544332211

Sample Output

Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

題解

思路

典型的大數相加,用字串解決,可以參考我寫的
基於字串的大數相加_C語言實現

注意事項

每個用例輸出都必須跟一個換行,另外,除了最後一個用例,其它用例還要再多一個換行,否則會報格式錯。

Code

#include <stdio.h>

int T;

char A[20][2000];
char B[20][2000];
char R[20][2000];

void Big_Number_Add( char A[], char B[], char R[] );

int main()
{
    int i ;
    scanf("%d",&T);

    for(i=0;i<T;i++)
    {
        scanf("%s%s",A[i],B[i]);
        Big_Number_Add(A[i],B[i],R[i]);
    }
    
    for(i=0;i<T;i++)
    {
        printf("Case %d:\n",i+1);
        printf("%s + %s = %s",A[i],B[i],R[i]);
        if(i!=T-1)
        printf("\n\n");
        else
        printf("\n");
    }
    
    return 0;
}

/**
  * @name   Big_Number_Add
  * @brief  基於字串的大數相加函式
  * @author Velscode 
  * @para   R[] is the string of result of A+B
  */
void Big_Number_Add( char A[], char B[], char R[] )
{
    long long int len_a = 0, len_b = 0,i,j;
    unsigned char carry_bit = 0, next_carry_bit = 0 ;
    char * p;
    
    //calculate length of A[]
    p = A;
    
    while( *p != '\0' )
    {
        p++;
        len_a++;
    }
    
    
    //calculate length of B[]
    p = B;
    
    while( *p != '\0' )
    {
        p++;
        len_b++;
    }
    
    if( len_a > len_b )
    {
        R[len_a+1] = '\0';
        
        for( i = 0; i < len_b; i++)
        {
            R[len_a-i] =      ( (A[len_a-1-i] - '0' + B[len_b-1-i] - '0') + carry_bit ) % 10 + 48;
            next_carry_bit =  ( (A[len_a-1-i] - '0' + B[len_b-1-i] - '0') + carry_bit ) / 10;
            carry_bit = next_carry_bit;
        }
        
        for( ; i < len_a; i++)
        {
            R[len_a-i] =      ( (A[len_a-1-i] - '0') + carry_bit ) % 10 + 48;
            next_carry_bit =  ( (A[len_a-1-i] - '0') + carry_bit ) / 10 ;
            carry_bit = next_carry_bit;
        }
        
        if( carry_bit != 0 )
            R[0] = carry_bit + 48;
        else
        {
            for(i=0;i<len_a;i++)
                R[i] = R[i+1];
        
            R[i] = '\0';
        }

    }
    
    if( len_a < len_b )
    {
        R[len_b+1] = '\0';
        
        for( i = 0; i < len_a; i++)
        {
            R[len_b-i] =      ( (A[len_a-1-i] - '0' + B[len_b-1-i] - '0') + carry_bit ) % 10 + 48;
            next_carry_bit =  ( (A[len_a-1-i] - '0' + B[len_b-1-i] - '0') + carry_bit ) / 10;
            carry_bit = next_carry_bit;
        }
        
        for( ; i < len_b; i++)
        {
            R[len_b-i] =      ( (B[len_b-1-i] - '0') + carry_bit ) % 10 + 48;
            next_carry_bit =  ( (B[len_b-1-i] - '0') + carry_bit ) / 10 ;
            carry_bit = next_carry_bit;
        }
        
        if( carry_bit != 0 )
            R[0] = carry_bit + 48;
        else
        {
            for(i=0;i<len_b;i++)
                R[i] = R[i+1];
        
            R[i] = '\0';
        }

    }
    
    if( len_a == len_b )
    {
        R[len_a+1] = '\0';
        
        for( i = 0; i < len_a; i++)
        {
            R[len_b-i] =      ( (A[len_a-1-i] - '0' + B[len_b-1-i] - '0') + carry_bit ) % 10 + 48;
            next_carry_bit =  ( (A[len_a-1-i] - '0' + B[len_b-1-i] - '0') + carry_bit ) / 10;
            carry_bit = next_carry_bit;
        }
        
        if( carry_bit != 0 )
            R[0] = carry_bit + 48;
        else
        {
            for(i=0;i<len_a;i++)
                R[i] = R[i+1];
        
            R[i] = '\0';
        }

    }
    
    
    return ;
}