码迷,mamicode.com
首页 > 其他好文 > 详细

大数求和

时间:2015-05-09 19:06:48      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:

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


int main()
{
	string A, B;
	cin>>A>>B;
	vector<int> vecSum;
	int lenA = A.length();
	int lenB = B.length();
	int temp;

	int carry =0;
	int indexA = lenA -1 , indexB = lenB -1;
	while(indexA >= 0 && indexB >= 0)
	{
		temp = carry + (A[indexA] -'0') + (B[indexB] - '0');
		vecSum.push_back(temp%10);
		carry = temp /10;
		indexA--;
		indexB--;
	}
	while(indexA >= 0)
	{
		temp = carry + (A[indexA] -'0');
		vecSum.push_back(temp%10);
		carry = temp /10;
		indexA--;
	}
	while(indexB >= 0)
	{
		temp = carry + (B[indexB] - '0');
		vecSum.push_back(temp%10);
		carry = temp /10;
		indexB--;
	}
	while(carry != 0)
	{
		vecSum.push_back(carry%10);
		carry /= 10;
	}
	for(vector<int>::reverse_iterator itr = vecSum.rbegin(); itr != vecSum.rend(); ++itr)
	{
		cout<<*itr;
	}
	cout<<endl;
	return 0;

}

大数求和

标签:

原文地址:http://blog.csdn.net/xiaohanstu/article/details/45603333

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!