洛谷P1601 A+B Problem(高精)(C++版)

原题转送门:https://www.luogu.com.cn/problem/P1601

题目描述

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

输入格式
分两行输入。a,b<=10500

输出格式
输出只有一行,代表a+ba+b的值

输入输出样例
输入

1
1

输出

2

代码:

#include<iostream>
#include<algorithm>
#include<string>
#include<stdio.h>
using namespace std;
int  a[501], b[501], c[501] = { 0 };
int main() {
	string stra, strb;
	cin >> stra >> strb;	
	//把字符串转成int型,并且倒叙存储
	for (int i = 0; i < stra.length(); i++)
	{
		a[i] = stra[stra.length() - i - 1] - '0';
	}
	for (int i = 0; i < strb.length(); i++)
	{
		b[i] = strb[strb.length() - i - 1] - '0';
	}
	int j;//记录结果的长度
	//加法的核心
	//得到长度最长的那个
	int len = stra.length() > strb.length() ? stra.length() : strb.length();
	for (j = 0; j < len; j++)
	{
		c[j] += a[j] + b[j];
		c[j + 1] = c[j] / 10;
		c[j] = c[j] % 10;
	}
	while (c[j] == 0 && j > 0)//使得最后一个零不被去除
	{
		j--;
	}
	for (int i = j; i >= 0; i--)
	{
		cout << c[i];
	}
	return 0;
}

欢迎任何形式的转载,但请务必注明出处。

限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教 。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值