【HDU】1002 A + B Problem II (string + 大数加法)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CSDN___CSDN/article/details/87197494

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

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

string sum(string s1,string s2)
{
	if(s1.length() < s2.length())
	{
		string t = s1;
		s1 = s2;
		s2 = t;
	}
	for(int i=s1.length()-1,j=s2.length()-1;i>=0;i--,j--)
	{
		s1[i] = char(s1[i] + (j>=0?s2[j]-'0':0));
		if(s1[i]-'0'>=10)
		{
			s1[i] = (s1[i]-'0')%10 + '0';
			if(i)
				s1[i-1]++;
			else
				s1 = "1" + s1;
		}
	}
	return s1;
}

int main ()
{
	string s1,s2;
	int T;
	cin >> T;
	for(int i=1;i<=T;i++)
	{
		cin >> s1 >> s2;
		cout << "Case " << i << ":\n";
		cout << s1 << " + " << s2 << " = " << sum(s1,s2) << endl ; 
		if(i!=T)
			cout << endl;
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/CSDN___CSDN/article/details/87197494