HDUOJ 1002

HDUOJ 1002 A+B problem Ⅱ


题目来源

题意简述

[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.

输入一个数T(1<=T<=20)表示有T个测试用例,接下来T行每行输入a和b两个数。输入的a和b均为正整数,但数字的长度不超过1000。给出a+b的和。

要点分析

本质就是大数加法的运用

严格的格式要求
1.输入为一次输入,输出也为一次输出。
2.每个输出结果之间有一行空行。
3.最后一个输出结果后有且仅有一个空行。

参考代码

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
	string a,b;
	vector<string> res[3];
	int t,r;
	cin>>t;
	while(t--)
	{
		cin>>a>>b;
		res[0].push_back(a);
		res[1].push_back(b);
		if(a.size()<b.size())
		{
			r=b.size()-a.size();
			for(int i=a.size()-1;i>=0;i--)
			{
				b[r+i]+=a[i]-'0';
				if(b[r+i]>'9')
				{
					b[r+i]-=10;
					b[r+i-1]++;
				}
			}
			for(int i=a.size()-1;i>=0;i--)
			{
				if(b[i]>'9')
				{
					b[i]-=10;
					b[i-1]++;
				}
			}
			if(b[0]>'9')
			{
				b[0]-=10;
				b.insert(0,1,'1');
			}
			res[2].push_back(b);
		}
		else
		{
			r=a.size()-b.size();
			for(int i=b.size()-1;i>=0;i--)
			{
				a[r+i]+=b[i]-'0';
				if(a[r+i]>'9' && r+i!=0)
				{
					a[r+i]-=10;
					a[r+i-1]++;
				}
			}
			for(int i=b.size()-1;i>=0;i--)
			{
				if(a[i]>'9' && i!=0)
				{
					a[i]-=10;
					a[i-1]++;
				}
			}
			if(a[0]>'9')
			{
				a[0]-=10;
				a.insert(0,1,'1');
			}
			res[2].push_back(a);
		}
	}
	for(int i=0;i<res[0].size();i++)
	{
		if(i!=0)cout<<endl;
		cout<<"Case "<<i+1<<":"<<endl;
		cout<<res[0][i]<<" + "<<res[1][i]<<" = "<<res[2][i]<<endl;
	}
	return 0;
}

20200702

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值