--- large integer array addition process

Topic links: http://ybt.ssoier.cn:8088/statusx.php?runidx=6655372

Description [title]

Non-negative integers the sum of two and no more than 200 bits.

[Enter]

There are two rows of not more than 200 is a non-negative integer, 0 may have redundant preamble.

[Output]

Line, i.e., the added result. The results can not have extra leading 0, that is, if the result is 342, you can not output to 0342.

[Sample input]

22222222222222222222
33333333333333333333

[Sample Output]

55555555555555555555

Code:

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	string s1;
	string s2;
	int i,add,a[500]={0},b[500],sum[500]={0},l,k,flag=0;
	cin>>s1>>s2;
	add=0,l=0;
	k=max(s1.length(),s2.length());
	for(i=s1.length()-1;i>=0;i--)
	{
		a[l]=s1[i]-'0';
		l++;
	}
	l=0;
	for(i=s2.length()-1;i>=0;i--)
	{
		b[l]=s2[i]-'0';
		l++;
	}
	for(i=0;i<k;i++)
	{
		sum[i]=a[i]+b[i]+add;
		add=sum[i]/10;
		sum[i]%=10;
	}
	if(add>0)
		sum[l++]=add;
	for(i=k-1;i>=0;i--)
	{
		if(sum[i]==0&&flag==0)
			continue;
		else
		{
			cout<<sum[i];
			flag=1;
		}
	}
	cout<<endl;
	return 0;
}

 

Published 36 original articles · won praise 27 · views 1620

Guess you like

Origin blog.csdn.net/weixin_44820625/article/details/104310052