B 奇怪的加法(模拟)

链接:https://ac.nowcoder.com/acm/contest/96/B
来源:牛客网

题目描述
zhrt是数学大佬,但有一天一道奇怪的数学题把他难住了:题目要求计算两个十进制数相加后的结果,但在本题中是不需要进位的!
现在zhrt弯下他的小蛮腰向你请教,你能帮帮他吗?
输入描述:
输入有多组(组数不超过1000),每组占一行,每行有2个正整数A和B。(A,B<=1e9)
输出描述:
每组输出占一行,输出题目中A和B相加的结果。
示例1
输入
复制
123 321
999 1
99 11
输出
复制
444
990
0

思路:就是模拟加法过程,算是一个简化过的大整数加法吧。不过写的过程还是不顺畅,码力还需要加强呢。

#include 

using namespace std;

int main()
{
	string str1,str2;
	string str3,str4;
	string str5;
	while(cin>>str1>>str2)
	{
		int res[1005];
		memset(res,0,sizeof(res));
		int len1 = str1.length();
		int len2 = str2.length();
		for(int i = 0;i < len1;i++)
		{
			str3[i] = str1[len1 - 1 - i];
		}
		for(int i = 0;i < len2;i++)
		{
			str4[i] = str2[len2 - 1 - i];
		}

		int len_max = max(len1,len2);
		if(len1 > len2)
		{
			for(int i = 0;i < len2;i++)
			{
				res[i] = (str3[i] + str4[i] - 2 * '0') % 10;
			}
			for(int i = len2;i < len1;i++)
			{
				res[i] = str3[i] - '0';
			}
		}
		else
		{
			for(int i = 0;i < len1;i++)
			{
				res[i] = (str3[i] + str4[i] - 2 * '0') % 10;
			}
			for(int i = len1;i < len2;i++)
			{
				res[i] = str4[i] - '0';
			}
		}

		while(res[len_max - 1] == 0 && len_max > 1)
		{
			len_max--;
		}

		for(int i = len_max - 1;i >= 0;i--)
		{
			cout<<res[i];
		}
		puts("");
	}
	return 0;
}

你可能感兴趣的:(B 奇怪的加法(模拟))