当前位置: 移动技术网 > IT编程>开发语言>C/C++ > A + B Problem II

A + B Problem II

2019年03月18日  | 移动技术网IT编程  | 我要评论

内置gps导航模块,directx9.0c64位,鬼吹灯之精绝古城下载迅雷

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

#define _crt_secure_no_warnings
#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;

int main()
{
    int n;
    cin >> n;
    getchar();
    int x = 1;
    while (n--)
    {
        string a, b;
        cin >> a;
        string c(a.rbegin(), a.rend());
        cin >> b;
        string d(b.rbegin(), b.rend());
        if (c.size() > d.size())
        {
            string e(d);
            d = c;
            c = e;
        }

        if (c.size() <= d.size())
        {
            d.push_back('0');
            for (int i = 0; i != c.size(); i++)
            {
                
                d[i] += c[i]-'0';
                if (d[i] > '9')
                {
                    d[i] -= 10;
                    d[i + 1] += 1;
                }
            }
            for (int i = 0; i != d.size(); i++)
            {
                if (d[i] > '9')
                {
                    d[i] -= 10;
                    d[i + 1] += 1;
                }
            }
            string e(d.rbegin(), d.rend());
            if (e[0] == '0')
                e = e.erase(0, 1);
            cout << "case " << x++ << ":" << endl;
            cout << a<<" + "<<b<<" = "<<e << endl;
            if (n != 0)
                cout << endl;
        }

    }
    //system("pause");
}

复习重点

字符串反转string c(a.rbegin(),a.rend())
字符串大小a.size()
字符串末位加字符a.push_back('0')
字符串删除字符a.erase(0,1) //从第0个起,删掉1个

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网