目录
- 题目
 - 输入
 - 输出
 - 样例输入
 - 样例输出
 - 解题思路
 - 参考代码
 - 附:c++ 大整数加法、减法、乘法
 - 总结
 
题目
输入
输入数据有多组。首先输入一个整数T,表示有T组输入。
每组输入两个大整数,并用空格隔开。每个整数最多1000位。没有负数输入。
输出
对于每组输入,输出两个整数的和,单独占一行。
样例输入
2 1 2 112233445566778899 998877665544332211
样例输出
3 1111111111111111110
解题思路
这个整体思路大家都知道,要注意的点就是在边界情况的处理上,比如进位的操作,最高位进位判断这些。还有可能有前导0的情况;
参考代码
#include<bits/stdc++.h>
using namespace std;
string get_add(string a, string b) {
    int i, add = 0, len = max(a.length(), b.length());
    int nums[1001] = {0};
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
    for(i = 0; i < len; i++) {
        int num1 = (i < a.length()) ? (a[i]-'0') : 0;
        int num2 = (i < b.length()) ? (b[i]-'0') : 0;
        int num = num1 + num2 + add;
        nums[i] = num % 10;
        add = num / 10;
    }
    if(add > 0) // 处理最高位进位
        nums[i] = add;
    string c;
    while(i >= 0) { // 数组逆序拼接得到结果
        c += to_string(nums[i]);
        i--;
    }
    if(c[0] == '0' && c.length() > 1) // 移除前导0
        c.erase(0, 1);
    return c;
}
int main() {
    int T;
    cin >> T;
    string a, b;
    getline(cin, a);
    while(T--) {
        cin >> a >> b;
        string c = get_add(a, b);
        cout << c << endl;
    }
    return 0;
}
附:c++ 大整数加法、减法、乘法
#include<string.h>
#include<iostream>
std::string add(std::string s1,std::string s2) {
	std::string s3;
	if(s1.length()<s2.length()) {
		while(s1.length()<s2.length()) {
			s1="0"+s1;
		}
	} else {
		while(s2.length()<s1.length()) {
			s2="0"+s2;
		}
	}
	int up=0;
	int k;
	int len=s1.length()-1;
	while(len>=0) {
		k=(s1.at(len) + s2.at(len) + up - '0' - '0');
		up = k/10;
		s3=(char)(k % 10 + '0') + s3;
		len--;
	}
	if(up)
		s3="1"+s3;
	return s3;
}
std::string sub(std::string s1,std::string s2) {
	if(s1==s2) {
		return "0";
	}
	std::string s3;
	bool flag=false;
	if(s1.length()<s2.length() || (s1.length()==s2.length() && s1<s2)) {
		std::string s=s1;
		s1=s2;
		s2=s;
		flag=true;
	} else {
		s3="";
	}
	while(s2.length()<s1.length()) {
		s2="0" + s2;
	}
	int k,down=0;
	for(int i=s1.length()-1; i>=0; i--) {
		k=s1[i] -s2[i] +down;
		if(k<0) {
			down=-1;
			k=10+k;
		} else {
			down=0;
		}
		s3=(char)('0' + k) + s3;
	}
	k=0;
	while(s3[k]=='0' ) {
		k++;
	}
	s3=s3.substr(k);
	if(flag)
		s3="-"+s3;
	return s3;
}
std::string mul(std::string s1,std::string s2) {
	if(s1=="0" || s2=="0")
		return "0";
	if(s1=="1")
		return s2;
	if(s2=="1")
		return s1;
	std::string s3;
	int len=s1.length() + s2.length() -1;
	for(int i=0; i<len; i++) {
		s3+="0";
	}
	int up=0,k;
	std::string flag="";
	for(int i=s1.length()-1; i>=0; i--) {
		for(int j=s2.length()-1; j>=0; j--) {
			k=s3[i+j]-'0' + (s1[i]-'0')*(s2[j]-'0') ;
			up=k/10;
			s3[i+j]=(char)(k%10+'0');
			int d=1;
			while(up>0) {
				//首位进位
				if(i==0 && j==0 && up>0) {
					flag=(char)("0"+ up);
					break;
				}
				k=(char)(s3[i+j-d]+up-'0');
				up=k/10;
				s3[i+j-d]=(char)(k%10+'0');
				d++;
			}
		}
	}
	return flag + s3;
}
总结
                        
	声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
		
评论(0)