ZOJ 1001 A + B Problem

ZOJ Problem Set - 1001

A + B Problem

Time Limit: 2 Seconds      Memory Limit: 65536 KB


Calculate a + b

Input

The input will consist of a series of pairs of integers a and b,separated by a space, one pair of integers per line.

Output

For each pair of input integers a and b you should output the sum of a and b in one line,and with one line of output for each line in input.

Sample Input

1 5

Sample Output

6

 

注:此题为:ZOJ 1001 A + B Problem

说明:此题就是一个普通的a+b问题,不是大数。

已AC源代码:

#include<iostream>
using namespace std;
int main()
{
	int a,b;
	while(cin>>a>>b)
	{
		cout<<a+b<<endl;
	}
	return 0;
}


以下是用大数写的,也可以AC:

#include<iostream>
#include<cstdio>
#include<cstring>
#define M 500
using namespace std;
char ch1[M],ch2[M];
int a[M],b[M];
int main()
{
	while(~scanf("%s%s",ch1,ch2))
	{
		int len,len1,len2,i,j;
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		len1=strlen(ch1);
		len2=strlen(ch2);
		for(i=0,j=len1-1;i<len1;++i,--j)
			a[j]=ch1[i]-'0';
		for(i=0,j=len2-1;i<len2;++i,--j)
			b[j]=ch2[i]-'0';
		len=len1>len2?len1:len2;
		for(i=0;i<len;++i)
		{
			a[i]+=b[i];
			if(a[i]>=10)
			{
				a[i+1]++;
				a[i]-=10;
			}
		}
		for(i=len+10;i>=0;--i)
			if(a[i]!=0)
				break;
		for(;i>=0;--i)
			cout<<a[i];
		cout<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值