欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

A + B Problem II

程序员文章站 2022-03-27 11:57:04
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 ......

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个