码迷,mamicode.com
首页 > 编程语言 > 详细

航电1002 A + B Problem II //算法

时间:2018-08-20 14:31:02      阅读:364      评论:0      收藏:0      [点我收藏+]

标签:ESS   some   you   bottom   res   ssi   put   --   esc   

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 429408    Accepted Submission(s): 83462


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
大数相加,注意格式
 1 #include<iostream>
 2 #include<cstring>
 3 #include<string>
 4 using namespace std;
 5 string bigAdd(string num1, string num2) {
 6 
 7     string res;
 8     if (num1.size() == 0) {
 9         res = num2;
10         return res;
11     }
12     if (num2.size() == 0) {
13         res = num1;
14         return res;
15     }
16 
17     res = "";
18     int n1 = num1.size()-1, n2 = num2.size()-1;
19     int carry = 0;
20     while (n1 >= 0 || n2 >= 0) {
21 
22         int a = n1 >= 0 ? num1[n1--] - 0 : 0;
23         int b = n2 >= 0 ? num2[n2--] - 0 : 0;
24 
25         int t = carry + a + b;
26         carry = t / 10;
27         t = t % 10;
28         res = to_string(t) + res;
29     }
30     //判断是否还有进位
31     while (carry) {
32         int t = carry / 10;
33         carry %= 10;
34         res = to_string(carry) + res;
35         carry = t;
36     }
37     return res;
38 }
39 
40 int main() {
41     int sum=0,count;
42     cin>>count;
43     if(count>=1&&count<=20) {
44         string a[22][2];
45         for(int i=0; i<count; i++) {
46             cin>>a[i][0]>>a[i][1];
47         }
48         for(int i=0; i<count; i++) {
49             cout<<"Case "<<i+1<<":"<<endl;
50             cout<<a[i][0]<<" + "<<a[i][1]<<" = "<<bigAdd(a[i][0],a[i][1])<<endl;
51             if(i+1!=count) {
52                 cout<<endl;
53             }
54         }
55     }
56     return 0;
57 }

 

 
 
 

航电1002 A + B Problem II //算法

标签:ESS   some   you   bottom   res   ssi   put   --   esc   

原文地址:https://www.cnblogs.com/yangxin6017/p/9505087.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!