高精度 A+B Problem

题目描述

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

输入格式

分两行输入。a,b ≤ 10^{500}

输出格式

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

输入输出样例

输入 #1

1
1

输出 #1

2

输入 #2

1001
9099

输出 #2

10100

方法1:结果用数组返回(推荐)

#include
using namespace std;

string s1,s2; 
int a[100001],b[100001],c[100001]; // 三个整数数组,用于存储转换后的数字
 
 
int main()
{
    cin>>s1>>s2; 
 
    int la = s1.length();
	int lb = s2.length();
 
    // 将字符串 s1 转换为逆序存储在数组 a 中
    for(int i=0;i0)l--;  //l>0:c数组下标是从0开始的
 
    // 输出结果
    for(int i=l;i>=0;i--)
    {
        cout<

方法2:结果用字符串返回

#include
#include
using namespace std;

//高精度加法 只能是两个正数相加 
string add(string str1, string str2)
{
	int len1=str1.length();
    int len2=str2.length();
    
	//补前导0,使字符串长度相等
	if(len1=0; i--)
	{
		temp = str1[i]-'0' + str2[i]-'0' + cf;
		cf = temp / 10;
		temp = temp % 10;
		str = char(temp+'0') + str; //字符串拼接 
	} 
	if(cf!=0) str = char(cf+'0')+str;
	return str;
}

int main()
{
	string str1, str2;
	cin>>str1>>str2; 
	cout<

你可能感兴趣的:(高精度加法)