方法1(自己写的适合初学者看)
#include<iostream>
#include<vector>
#include<sstream>
#include<algorithm>
using namespace std;
string solve(string a, string b) {
string res_str="";
int add_pos = 0;
if (a == "0" || a == "") {
if (b == "0" || b == "") {
return "0";
}
else {
return b;
}
}
if (b == "0" || b == "") {
if (a == "0" || a == "") {
return "0";
}
else {
return a;
}
}
for (int i = 0; i <= max(a.length(), b.length()); i++) {
int num_a = 0;
int num_b = 0;
int len_a = a.length(), len_b = b.length();
if (i == max(a.length(), b.length()) && add_pos == 1) {
res_str += add_pos + '0';
reverse(res_str.begin(), res_str.end());
return res_str;
}
if (i < min(a.length(), b.length())) {
if (add_pos == 0) {
num_a = a[len_a - i-1] - '0';
num_b = b[len_b - i-1] - '0';
int sum = 0;
sum = num_a + num_b;
if (sum >= 10) {
int res_value = 0;
res_value = sum - 10;
res_str += res_value + '0';
add_pos = 1;
}
else {
res_str += sum + '0';
add_pos = 0;
}
}
else {
num_a = a[len_a - i-1] - '0';
num_b = b[len_b - i-1] - '0';
int sum = 0;
sum = num_a + num_b + 1;
if (sum >= 10) {
int res_value = 0;
res_value = sum - 10;
res_str += res_value + '0';
add_pos = 1;
}
else {
res_str += sum + '0';
add_pos = 0;
}
}
}
else {
if (add_pos != 1) {
if (len_a >=len_b) {
res_str += a.substr(0, len_a - i);
reverse(res_str.begin(), res_str.end());
return res_str;
}
else {
res_str += b.substr(0, len_b - i);
reverse(res_str.begin(), res_str.end());
return res_str;
}
}
else {
if (len_a >= len_b) {
num_a = a[len_a - i-1] - '0';
num_a += 1;
if (num_a >= 10){
int res_value = 0;
res_value = num_a - 10;
res_str += res_value + '0';
add_pos = 1;
}
else {
res_str += num_a + '0';
res_str += a.substr(0, len_a - i);
reverse(res_str.begin(), res_str.end());
return res_str;
}
}
else {
num_b = b[len_b - i-1] - '0';
num_b += 1;
if (num_b >= 10) {
int res_value = 0;
res_value = num_b - 10;
res_str += res_value + '0';
add_pos = 1;
}
else {
res_str += num_b + '0';
res_str += b.substr(0, len_b - i);
reverse(res_str.begin(), res_str.end());
return res_str;
}
}
}
}
}
}
int main() {
string a,b,s;
cin >> a;
cin >> b;
s = solve(a, b);
cout << s;
return 0;
}
笔记:
1.数字字符转整型数字:‘数字字符’-‘0’;数字转数字字符:数字+‘0’;
2.使用 std::max
(max)或 std::min
(min)函数来比较两个数字的大小,需要包含头文件 #include<algorithm>
3.reverse(res_str.begin(), res_str.end());反转字符串res_str.
方法2
#include<iostream>
#include<vector>
#include<sstream>
#include<algorithm>
using namespace std;
string solve(string s, string t) {
//s为空或'0'返回t
if (s.empty()) {
return t;
}
//t为空或'0'返回s
if (t.empty()) {
return s;
}
//令较长的字符串为s
if (s.length() < t.length()) {
swap(s, t);
}
//add_pos表示是否进位
int add_pos = 0;
//for循环从后往前遍历长串s
for (int i = s.length() - 1; i >= 0; i--) {
int temp = 0;
//转数字再加进位
temp = s[i] - '0' + add_pos;
//修改字符串s[i]的字符
s[i] = temp + '0';
//从后往前依次遍历短串t下标,使得短串t的小标与s对齐
int j = i - s.length() + t.length();
//判断短串是否还有,有加短串,没有不加短串
if (j >= 0) {
temp= t[j] - '0' + temp;
}
//进位
add_pos = temp / 10;
//取余修改s[i]
temp = temp % 10;
s[i] = temp + '0';
}
//长串s遍历结束,判断最高位是否需要进位,进位则在s最前段加'1';
if (add_pos == 1) {
s= '1' + s;
}
return s;
}
int main() {
string a,b,s;
cin >> a;
cin >> b;
s = solve(a, b);
cout << s;
return 0;
}
笔记:
1.swap(a,b):交换两个a,b两个变量的值。C/C++编程笔记:C++中的 swap 内置函数,用法详解_c++ std::swap-CSDN博客