【HDOJ】 <Problem - 1002> : A + B Problem II


原题地址: [ http://acm.hdu.edu.cn/showproblem.php?pid=1002 ]

代码地址: [ http://download.csdn.net/detail/angryn00b/5054960 ]


Problem Description 

给出2个数 分别为计算A + B的和

Input 

首先输入测试次数 

然后输入参与计算的2个数 和 B

注意这2个数会很大 这意味着32位整形将无法正确表示它们
你必须要自己控制它们的长度 不能长度超过1000

Output

每次测试你必须输出对应的2

第一行应当输出“Case #:#’符号为第几次的输入 

第二行应当输出“A + B = Sum” SumA+B的和,请留意表达式中的空格

最后要保证每2个测试之间要有一个空行


Sample Input 

2

1 2

112233445566778899 998877665544332211

Sample Output

Case 1:

1 + 2 = 3


Case 2:

112233445566778899 + 998877665544332211 = 1111111111111111110



代码

/*
* Description: A+B problem
* Author : Angryn00b
*/

#include <iostream>
#include <string>
#include <vector>
using namespace std;

//输入字符串 把字符串转换成数字 存放入数组中
///
bool input (istream & is, string &_src, vector<int> &_dest);

//重载 << 用于输出 数组中的数
///
ostream & operator<<(ostream &os, const vector<int> &_dest);

// 计算A+B的和 并且去除存在0的首项
vector<int> sum(const vector<int> &_src1, const vector<int> &_src2);
int main(void)
{
	
	//lines 为 输入的行数 
	//n 用于迭代
	int lines, n = 0; 
	cin >> lines;
	while (n < lines)
	{ 
		vector<int> A; // 存放A的大数
		string a;      
		if (!input(cin, a, A)) // 字符串转换成数字存入数组
			break;

		vector<int> B; // 存放B的大数
		string b;
		if (!input(cin, b, B)) // 字符串转换成数字存入数组
			break;
		
		cout << "Case " << n+1 << ":" << endl;
		cout << A << " + " << B << " = " << sum(A,B) << endl;
		
		if (n  != lines-1) // 处理空行
			cout << endl;
		++n;
	}

	return 0;
}

bool input (istream & is, string &_src, vector<int> &_dest)
{
	if (is >> _src, is) {

		for (string::const_iterator 
			ix = _src.begin(); ix != _src.end(); ++ix)		
				_dest.push_back(*ix-'0');

		return true;
	}

	else

		return false;
}

ostream & operator<< (ostream &os, const vector<int> &_dest)
{
	
	for (vector<int>::const_iterator 
		ix = _dest.begin(); ix != _dest.end(); ++ix)
			cout << *ix;
	return os;
}

vector<int> sum(const vector<int> &_src1, const vector<int> &_src2)
{
	int size = _src1.size() > _src2.size() ? _src1.size() : _src2.size();
	vector<int> _sum(size+1, 0); //由于存在 99+1 = 100  所以要预留一个长度
	int _is1 = _src1.size()-1;
	int _is2 = _src2.size()-1;
	for (int ix = size; ix >= 0; ix--)
	{
		if (_is1 >= 0) {
			_sum[ix] += _src1[_is1];
			--_is1;
		}

		if  (_is2 >= 0) {
			_sum[ix] += _src2[_is2];
			--_is2;
		}

		if (_sum[ix] >= 10)
		{
			_sum[ix-1] = _sum[ix]/10;
			_sum[ix] = _sum[ix]%10;
		}
	}
	
	// 当长度没有增加时 去除预留的长度
	if (_sum[0] == 0)
			_sum.erase(_sum.begin());

	return _sum;
}




AC

1. 字符形式读入数据 并且使用数组来存放数据 然后相加

   这里我采用的是string(负责读入字符) vector<int>(把字符转换成数字存储) 来实现

   个位存放在数组的末尾项 以此类推 高位存放在首项

2. 99 + 1 = 100 会变成3位数 所以需要多预留一个空间来存放 可能产生的进位

3. 注意题目中的空行是出现在每2个测试的结果之间 意味着最后一次测试就不会输出空行


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值