桑海

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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
 
View Code
 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 
 5 #define P(x) \
 6 cout << #x " " << x << ":" << endl;
 7 
 8 #define PE(a, b, c, sum) \
 9 cout << a << " + " <<  b << " = " << (c ? "1" : "") << sum << endl;
10 int main()
11 {
12     int T;
13     cin >> T;
14     int Case = 1;
15     while(T--)
16     {
17         P(Case);
18         ++Case;
19         string a, b, sum;
20         cin >> a >> b;
21         //cout << a << " + " <<  b << " = " ;
22         int l_a = a.size()-1, l_b = b.size()-1, c = 0;
23         sum = (l_a > l_b ? a : b);
24         int i, j;
25         for(i = l_a, j = l_b; i >= 0 && j >= 0; --i, --j)
26         {
27             int s  = a[i] - '0' + b[j] - '0' + c;
28             sum[i>j?i:j] = s % 10 + 48;
29             c = s / 10;
30         }
31         while(i >= 0)
32         {//处理进位
33             int s = sum[i] - '0' + c;
34             sum[i] = s % 10 + 48;
35             c = s / 10;
36             i--;
37         }
38         while(j >= 0)
39         {//处理 进位
40             int s = sum[j] - '0' + c;
41             sum[j] = s % 10 + 48;
42             c = s / 10;
43             j--;
44         }
45         // cout << (c ? "1" : "") << sum << endl;
46         PE(a, b, c, sum);
47         if(T)
48             cout << endl;
49     }
50     return 0;
51 }

 

问题探究
#include<iostream>
#include<string>
using namespace std;

const int maxn = 500;
#define P(x) \
cout << #x ": " << x << endl;
int main()
{
    string a;
    cin >> a;
    //string str;
    //string str("shi");
    string str(maxn, 'a');
    //str = ""; //长度也变为了0
    for(int i = 0; i != a.size(); ++i)  //the same above
    //for(int i = a.size()-1; i >= 0; --i)
        str[i] = a[i];
    P(a);
    P(str);
    return 0;
}

/*
str: ""
output: ""

str:"shi"
output: san

由此可见,string 对象是有默认长度的(根据其赋值对象的长度决定)
与字符数组类似的,只不过不需明确指明,可根据赋值对象动态的适应并
改变它。字符赋值,其实质是在原有的基础上修改,是有长度限制的。
但是后面以后str = "";以后长度也都变为了0.由此可见,使用字符赋值的
关键是如何确定他的可能长度。
*/

 

posted on 2013-02-22 10:10  桑海  阅读(550)  评论(0编辑  收藏  举报