P1601 A+B Problem(高精)

# A+B Problem(高精)

## 题目描述

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

## 输入格式

分两行输入。$a,b \leq 10^{500}$。

## 输出格式

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

## 样例 #1

### 样例输入 #1

```
1
1
```

### 样例输出 #1

```
2
```

## 样例 #2

### 样例输入 #2

```
1001
9099
```

### 样例输出 #2

```
10100
```

#include<iostream>
#include<string>
#include<vector>
#include<cmath>
using namespace std;
void add(string a,string b)
{
	int len1 = a.length(), len2 = b.length();
	if (len1 > len2)
	{
		for (int i = 0; i < len1 - len2; i++)
			b = "0" + b;
		len2 = len1;
	}
	else
	{
		for (int i = 0; i < len2 - len1; i++)
			a = "0" + a;
		len1 = len2;
	}
	string c(len1, 0);
	int units = 0, tens = 0, temp = 0;
	for (int i = len2 - 1; i >= 0; i--)
	{
		temp = a[i] - '0' + b[i] - '0' + tens;
		units = temp % 10, tens = temp / 10;
		c[i] = units + '0';
	}
	if (tens > 0)
	{
		c = "0" + c;
		c[0] = tens + '0';
	}
	cout << c << endl;
}
int main() {
	string a, b;
	cin >> a >> b;
	add(a, b);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值