原题转送门: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;
}
欢迎任何形式的转载,但请务必注明出处。
限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教 。