#P1601 A+B Problem(高精)

P1601 A+B Problem(高精)

题目描述

高精度加法,相当于 a+b problem,不用考虑负数

输入格式

分两行输入。 a , b ≤ 1 0 500 a,b \leq 10^{500} a,b10500

输出格式

输出只有一行,代表 a + b a+b a+b 的值。

样例 #1

样例输入 #1

1
1

样例输出 #1

2

样例 #2

样例输入 #2

1001
9099

样例输出 #2

10100

提示

20 % 20\% 20% 的测试数据, 0 ≤ a , b ≤ 1 0 9 0\le a,b \le10^9 0a,b109

40 % 40\% 40% 的测试数据, 0 ≤ a , b ≤ 1 0 18 0\le a,b \le10^{18} 0a,b1018

代码

#include<bits/stdc++.h>
using namespace std;
char sa[1010], sb[1010];
int la, lb, lc, a[1010], b[1010], c[1010];
int main(){
	scanf("%s",sa);
	scanf("%s",sb);
	la = strlen(sa);
	lb = strlen(sb);
	memset(a, 0, sizeof(a));
	memset(b, 0, sizeof(b));
	for(int i = 0; i < la; i++)
		a[la-i-1] = sa[i] - '0';
	for(int i = 0; i < lb; i++)
		b[lb-i-1] = sb[i] - '0';
	lc = la > lb ? la : lb;
	memset(c,0,sizeof(c));
	for(int i = 0; i < lc; i++){
		c[i] += a[i] + b[i];
		if(c[i] >= 10){
			c[i+1] = 1;
			c[i] -= 10;
		}		
	}
	if(c[lc]>0) lc++;
	for(int i = lc - 1; i >= 0; i--)
		printf("%d",c[i]);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值