HDU-1002-A+B

/*转载请注明出处:乄心-小黄豆http://blog.csdn.net/wuxinxiaohuangdou*/

题目大意:计算A+B,只不过不能直接用 整型!因为是1000以内的大数相加,整型表示不了!

所以只可以用字符串处理咯!

本题解题思路:用两个string保存,然后从两个string的尾部对应相加,然后再注意下进位就ok了!

一定要注意格式!格式坑了我一次!大哭

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
using namespace std;
int opp[1010];
int sum;
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int T;
	int cas=1;
	scanf("%d",&T);
	while(T--)
	{
	string a,b;
	cin>>a>>b;
	printf("Case %d:\n",cas++);
	cout<<a<<" + "<<b<<" = ";     //注意格式!+与= 两边都有空格!
		sum=0;
		string::iterator ita,itb;
		reverse(a.begin(),a.end());    //先反转一下。
		reverse(b.begin(),b.end());
		int xa,xb;
		int temp=0;
		for(ita=a.begin(),itb=b.begin();ita!=a.end()&&itb!=b.end();ita++,itb++)
		{
			char ch=(*ita);
			xa=ch-'0';
			ch=(*itb);
			xb=ch-'0';
			opp[++sum]=(xa+xb+temp)%10;
			temp=(xa+xb+temp)/10;
		}
		int first=1;
		for(;ita!=a.end();ita++)      //处理两个数数位不相等的情况!
		{
			if(temp==1)
			{
				int xc=*ita-'0';
				opp[++sum]=(xc+temp)%10;
				temp=(temp+xc)/10;
			}
			else
				opp[++sum]=*ita-'0';
		}
		for(;itb!=b.end();itb++)     //处理两个数数位不相等的情况!
		{
			if(temp==1)
			{
				int xc=*itb-'0';
				opp[++sum]=(xc+temp)%10;
				temp=(temp+xc)/10;
			}
			else
				opp[++sum]=*itb-'0';
		}
		if(temp==1)                     //最后可能还有一个进位要加进去哦!
			opp[++sum]=temp;
		
		for(int i=sum;i>=1;i--)
			cout<<opp[i];
		if(T==0)                    //注意格式!每两个案例间多一个换行,最后一个不要换行!
		cout<<endl;
		else
			cout<<endl<<endl;
	
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值