60分,第3、5个不对,求教!
查看原帖
60分,第3、5个不对,求教!
1546391
n_sc楼主2024/11/19 21:50
#include <bits/stdc++.h>
using namespace std;
int main()
{
	string sa, sb;
	int a[520], b[520];
	int c[520];
	char t;
	int la = 0, lb = 0;
	cin >> sa >> sb;
	la = sa.length();
	lb = sb.length();
	for (int i = 0; i < la; i++)
		a[i] = sa[la - i - 1] - '0';
	for (int i = 0; i < lb; i++)
		b[i] = sb[lb - i - 1] - '0';
	int minn = min(la, lb);
	int maxx = max(la, lb);
	c[0] = 0;
	for (int i = 0; i < minn; i++)
	{
		c[i + 1] = (c[i] + a[i] + b[i]) / 10;
		c[i] = (c[i] + a[i] + b[i]) % 10;
	}
	if (la > lb)
	{
		c[minn] += a[minn];
		for (int i = minn + 1; i < maxx; i++)
			c[i] = a[i];
	}
	if (la < lb)
	{
		c[minn] += b[minn];
		for (int i = minn + 1; i < maxx; i++)
			c[i] = b[i];
	}
	if (c[maxx] > 0)
		cout << c[maxx];
	for (int i = maxx - 1; i >= 0; i--)
		cout << c[i];
}
2024/11/19 21:50
加载中...