一、热身 [Cloned] Z - A + B Problem II

原题:

I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. 

题意:

输入两个大数求和。

题解:

就是大数加和的问题,用字符串储存,然后相当于竖式加法,注意进位和输出格式的问题就ok了

代码:AC

#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int max(int a,int b)
{
	if(a>b)
		return a;
	else
		return b;
}
char A[1020];
char B[1020];
char C[1020];
char D[1020];
char E[1020];
int main()
{
	int n;
	cin>>n;
	int i;
	for(i=0;i<n;i++)
	{
		if(i)
			cout<<endl;
		cin>>A>>B;
		int a=strlen(A);
		int b=strlen(B);
		memset(C,'0',sizeof(C));
		memset(D,'0',sizeof(D));
		memset(E,'0',sizeof(E));
		int j,k=1020;
		for(j=0;j<a;j++)
		{
			C[k-a]=A[j];
			k++;
		}
		k=1020;
		for(j=0;j<b;j++)
		{
			D[k-b]=B[j];
			k++;
		}
		int maxn=max(a,b)+1;
		int sum=0;
		for(j=1019;j>=1019-maxn-1;j--)
		{
			E[j]=(C[j]+D[j]-2*'0'+sum)%10+'0';
			if((C[j]+D[j]-2*'0'+sum)>=10)
				sum=1;
			else
				sum=0;
		}
		for(j=0;j<1020;j++)
		{
			if(E[j]!='0')
				break;
		}
		cout<<"Case "<<i+1<<":"<<endl;
		cout<<A<<" + "<<B<<" = ";
		for(;j<1020;j++)
			cout<<E[j];
		cout<<endl;
	}
	return 0;
}	
	

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值