Luogu P1601 A+B Problem (High Precision)

topic background

without

Topic description

High-precision addition, x is equivalent to the a+b problem, [b][color=red]Do not consider negative numbers[/color][/b]

Input and output format

Input format:

Enter a,b<=10^500 in two lines

Output format:

The output has only one line, representing the value of A+B

Input and output example

Input Example #1:   Copy
1
1
Output Sample #1:   Copy
2





#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
#define Maxn 1000
char a1[Maxn];
char b1[Maxn];
int c[Maxn];
int a[Maxn];
int b[Maxn];
intmain()
{
	cin >> a1 >> b1;
	int lena = strlen(a1);
	int lenb = strlen(b1);

	//Convert the string to int type in reverse order
	for (int i = 0; i <= lena-1; i++)
		a[lena - i] = a1[i] - '0';
	for (int i = 0; i <= lenb-1; i++)
		b[lenb - i] = b1[i] - '0';

	int lenc = 1;//string length
	int x = 0;//carry

	//After this loop is executed, lenc will be equal to max(lena, lenb)+1, if you don't believe me, you can try it yourself
	while (lenc <= lena || lenc <= lenb)
	{
		c[lenc] = a[lenc] + b[lenc] + x;
		x = c[lenc] / 10;
		c[lenc] %= 10;
		lenc ++;
	}
	if (x == 0)lenc--;//If the carry is 0 after processing, reduce the length by 1
	else c[lenc] = x;//Increase the highest bit if it is not 0

	for (int i = lenc; i >= 1; i--)//输出
		cout << c[i];
	cout << endl;
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324768902&siteId=291194637