idea
- 测试点3:输入的两个整数都是0
- 测试点4.5:大数,需要用大数加法
solution1(测试点4,5不通过)
直接相加再转30进制
#include<iostream>
#include<string>
using namespace std;
typedef long long ll;
string a, b, sum = "", temp;
ll change(string s){ll power = 1, ans = 0;for(int i = s.size() - 1; i >= 0; i--){if(isdigit(s[i])) ans += (s[i] - '0') * power;else ans += (s[i] - 'a' + 10) * power;power *= 30;}return ans;
}
int main(){ll an, bn, sumn, t;cin >> a >> b;an = change(a);bn = change(b);sumn = an + bn;do{t = sumn % 30;temp = t - 10 + 'a';if(t < 10) sum = to_string(t) + sum;else sum = temp + sum;sumn /= 30;}while(sumn);cout << sum;return 0;
}
solution2
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){string a, b, sum = "";int ta, tb, tsum, carry = 0;cin >> a >> b;if(a.size() < b.size())swap(a, b);while(b.size() < a.size()) b = "0" + b;for(int i = a.size() - 1; i >= 0; i--){if(isdigit(a[i])) ta = a[i] - '0';else ta = a[i] - 'a' + 10;if(isdigit(b[i])) tb = b[i] - '0';else tb = b[i] - 'a' + 10;tsum = (ta + tb + carry) % 30;carry = (ta + tb + carry) / 30;if(tsum < 10) sum += to_string(tsum);else sum += tsum - 10 + 'a';}if(carry) sum += "1";reverse(sum.begin(), sum.end());while(sum[0] == '0' && sum.size() > 1) sum.erase(0, 1);cout << sum;return 0;
}