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

HDUacm 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
 
Code:
 1 #include <stdio.h>
 2 #include <string.h>
 3 main()
 4 {
 5     char a1[1001]={'\0'},b1[1001]={'\0'};
 6     int a2[1001],b2[1001],sum[1001];
 7     int n,i;        //n 總次數,i 第i次
 8     int la,lb,j,k,m,r;
 9
10     scanf("%d",&n);
11     i=1;
12     while(i<=n)
13     {
14         for(j=0;j<1001;j++) //初始化
15             sum[j]=0;
16         for(j=0;j<1001;j++)
17             a2[j]=0;
18         for(j=0;j<1001;j++)
19             b2[j]=0;
20         scanf("%s",&a1);
21         scanf("%s",&b1);
22         la=strlen(a1);
23         lb=strlen(b1);
24         r=1;
25         for(j=la-1;j>=0;j--) //類型轉換,將數字位置逆序
26         {
27             a2[r]=a1[j]-48;
28             r++;
29         }
30         r=1;
31         for(j=lb-1;j>=0;j--)
32         {
33             b2[r]=b1[j]-48;
34             r++;
35         }
36
37         if(la>lb)  //以較大數的長度進行相加
38             k=la;
39         else
40             k=lb;
41         for(j=1;j<=k;j++)
42         {
43             sum[j]=sum[j]+a2[j]+b2[j];
44             if(sum[j]>=10)  //進位
45             {
46                 sum[j]=sum[j]-10;
47                 m=j+1;
48                 sum[m]++;
49             }
50         }
51        
52         printf("Case %d:\n",i);  //輸出結果,注意數字輸出順序
53         for(j=la;j>0;j--)
54         {
55             printf("%d",a2[j]);
56         }
57         printf(" + ");
58         for(j=lb;j>0;j--)
59         {
60             printf("%d",b2[j]);
61         }
62         printf(" = ");
63         if(sum[k+1])
64             k++;
65         for(j=k;j>0;j--)
66         {
67             printf("%d",sum[j]);
68         }
69         printf("\n");
70         if(i<n)
71             printf("\n");
72    
73         i++;
74     }
75 }


Tips:
    1、本題為大整數相加類型,我先將大整數作為字符串讀取,然後逐位轉換為int型數,最後按位模擬求值;
    2、多次讀取字符時,務必注意對數組進行初始化操作(為此我犧牲了近半小時的時間debug);
    3、注意可能存在的最高位的進位輸出;
    4、Output a blank line between two test cases. --需要判斷Case數目;
    5、You may assume the length of each integer will not exceed 1000. --將數組定義的大於題目范圍,防止溢出;
    6、字符串或char型數組的初始化可以使用‘\0’。

 

作者 任琦磊
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved