A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 158615 Accepted Submission(s): 30098
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
算法很简单,小学老师就教过,只不过用C++语言实现了一遍。
1 #include <iostream>
2
3 using namespace std;
4
5 int main()
6 {
7 char num1[1001],num2[1001],res[1002];
8 int T,i,len1,len2,count,res_digit,digit_sum,C=0;
9 //输入实验次数T
10 cin>>T;
11 while(T--){ //循环T次
12 C++;
13 res[0]='0';
14 cin>>num1; //输入第一个数,存储进num1(char [1001])
15 cin>>num2; //输入第二个数,存储进num2(char [1001])
16 //计算第一个数长度(位数),存储进len1
17 for(len1=0;num1[len1]!='\0';len1++);
18 //计算第二个数长度(位数),存储进len2
19 for(len2=0;num2[len2]!='\0';len2++);
20 //进行相加运算,存储进res(char [1002])中。
21 //计算结果应该有的位数
22 if(len1>=len2) res_digit=len1;
23 else res_digit=len2;
24 res[res_digit+1]='\0'; //先给结果附上结束符
25 len1--;
26 len2--;
27 //循环相加,len1和len2不断--,直到其中一个减到0
28 count=0;
29 while(len1!=-1 && len2!=-1){
30 if(count==0)
31 digit_sum=(num1[len1]-'0')+(num2[len2]-'0');
32 else
33 digit_sum=(num1[len1]-'0')+(num2[len2]-'0')+count;
34 if(digit_sum>9){ //如果数字之和为2位数
35 res[res_digit]=char(digit_sum%10+'0');
36 count=1;
37 }
38 else{ //如果数字之和为个位数
39 res[res_digit]=char(digit_sum+'0');
40 count=0;
41 }
42 res_digit--;
43 len1--;
44 len2--;
45 }
46
47 if(len1==-1 && len2==-1){
48 cout<<"Case "<<C<<':'<<endl;
49 if(count==0){
50 cout<<num1<<' '<<'+'<<' '<<num2<<' '<<'='<<' ';
51 //输出结果,不带开头的0
52 for(i=1;res[i]!='\0';i++)
53 cout<<res[i];
54 cout<<endl;
55 }
56 else{
57 cout<<num1<<' '<<'+'<<' '<<num2<<' '<<'='<<' ';
58 //输出结果,带第一个
59 res[0]='1';
60 cout<<res<<endl;
61 }
62 }
63 else if(len1==-1 && len2!=-1){
64 if(count==0){
65 res[res_digit]=num2[len2];
66 res_digit--;
67 len2--;
68 }
69 else{
70 res[res_digit]=char(num2[len2]-'0'+count+'0');
71 res_digit--;
72 len2--;
73 }
74 while(res_digit!=0){
75 res[res_digit]=num2[len2];
76 len2--;
77 res_digit--;
78 }
79 cout<<"Case "<<C<<':'<<endl;
80 cout<<num1<<' '<<'+'<<' '<<num2<<' '<<'='<<' ';
81 for(i=1;res[i]!='\0';i++)
82 cout<<res[i];
83 cout<<endl;
84 }
85 else{ //len2==-1 && len1!=-1
86 if(count==0){
87 res[res_digit]=num1[len1];
88 res_digit--;
89 len1--;
90 }
91 else{
92 res[res_digit]=char(num1[len1]-'0'+count+'0');
93 res_digit--;
94 len1--;
95 }
96 while(res_digit!=0){
97 res[res_digit]=num1[len1];
98 len1--;
99 res_digit--;
100 }
101 cout<<"Case "<<C<<':'<<endl;
102 cout<<num1<<' '<<'+'<<' '<<num2<<' '<<'='<<' ';
103 for(i=1;res[i]!='\0';i++)
104 cout<<res[i];
105 cout<<endl;
106 }
107 if(T!=0)
108 cout<<endl;
109 }
110 return 0;
111
112 //最后加一个'\0'
113 //按格式输出res[1002]
114 //如果res[0]有值,则从0输出
115 //如果res[0]是0,则从1输出
116 }
Freecode : www.cnblogs.com/yym2013