1. 程式人生 > >【HDUOJ】第1002題 A + B Problem II 純C語言解法

【HDUOJ】第1002題 A + B Problem II 純C語言解法

【HUDOJ-1002】

1.原題:

Problem DescriptionI have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

InputThe 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.

OutputFor 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 Input21 2112233445566778899 998877665544332211
Sample OutputCase 1:1 + 2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110
AuthorIgnatius.L

2:解題思路及技巧:

在計算機中,當要表示的資料超出計算機所使用的資料的表示範圍時,則產生資料的溢位。溢位導致大數相加計算錯誤,此時就需要一種新的辦法。由於大數無法直接儲存,我們採用字串的方式儲存大數的每一位,然後將字元轉換為數字型,類似於小學加法算式將兩個數對應位相加(大於十進一),這裡需要注意以下幾點:

a.字元轉數字:遍歷陣列對每一位減‘0’得到對應數字的integer值。

b.要初始化所有陣列的所有元素為0以便後續的計算,這裡採用memset函式將陣列的所有元素初始化為0。

c.存數時需要倒序輸入便於計算。

d.memset函式:memset是計算機中C/C++語言函式。將s所指向的某一塊記憶體中的後count個位元組的內容全部設定為ch指定的

ASCII值, 第一個值為指定的記憶體地址,塊的大小由第三個引數指定,這個函式通常為新申請的記憶體做初始化工作, 其返回值為s。

  1. void * memset(void * s,int ch,size_t count)  
  2. {  
  3.     char *xs = (char *) s;  
  4.     while (count--)  
  5.         *xs++ = ch;  
  6.     return s;  
  7. }  

3:完整程式碼(已AC

#include <stdio.h>
#include <string.h>
int main(int argc,char const*argv[])
{
    int T;
    char m[1001],n[1001];
    scanf("%d",&T);
    int cnt=1;
    int k=T;
    while(k--){
        int a[1001],b[1001],c[1001];
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
       //初始化陣列元素為零
        int l,len;
        scanf("%s",&m);
        scanf("%s",&n);
        //讀入字元陣列
        int len1=strlen(m),len2=strlen(n);
        //儲存字元型數字的長度
        int max=(len1>len2)?len1:len2;
        for(int i=0;i<len1;i++)            a[i]=m[len1-i-1]-'0';
        for(int j=0;j<len2;j++)
            b[j]=n[len2-j-1]-'0';
        //將字元轉換為integer型數字並存入陣列
        for( len=0;len<max;len++){
            if(a[len]+b[len]+c[len]>=10){
                c[len]+=a[len]+b[len]-10;
                c[len+1]++;
            }
            else
                c[len]+=a[len]+b[len];
        }
        for(l=len;l<max;l++){
            if(a[l]+c[l]>=10){
                c[l]+=a[l]-10;
                c[l+1]++;
            }
            else
            c[l]+=a[l];
        }
        printf("Case %d:\n",cnt);
        for(int i=len1-1;i>=0;i--)
            printf("%d",a[i]);
        printf(" + ");
        for(int i=len2-1;i>=0;i--)
            printf("%d",b[i]);
        printf(" = ");
        for(int x=l-1;x>=0;x--)
            printf("%d",c[x]);
        if(cnt++<T)
            printf("\n\n");
        else
            printf("\n");
    }
    return 0;
}