设为首页 加入收藏

TOP

A + B Problem II
2019-03-17 12:17:07 】 浏览:67
Tags:Problem

http://acm.hdu.edu.cn/showproblem.php?pid=1002

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个

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Java之数据类型 下一篇简单数学(组合数+求数列通项公式..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目