思路:利用高精度加法求出和然后根据每一位要求的进制进行计算 前四个过了,自己造了一个发现不对
样例:
输入:120062+2150
标准输出:1,0,2,2,0
实际输出:1,2,2,2,0
代码:
#include<iostream>
#include<cstring>
#include<climits>
using namespace std;
string s;
int sy[10] = {0 , 2 , 3 , 5 , 7 , 11 , 13}; // 对应 个 十 百 千 万 十万
int as = INT_MAX , bs = INT_MAX , f;
int a[102034] , b[102034] , c[102034] , d[102034];
string v; string w;
int len;
void jia(){
len = max(v.length() , w.length());
for(int i = len; i >= 1; i --){
c[i] = a[i] + b[i];
}
int len1 = 1;
for(int i = len1; i <= len; i ++){
if(c[i] >= 10){
c[i + 1] += c[i] / 10;
c[i] %= 10;
}
}
while(c[len + 1] != 0) len ++;
// for(int i = len; i >= 1; i --) cout << c[i];
}
int main(){
cin >> s;
for(int i = 0; i < s.length(); i ++){
if(s[i] == '+') f = 1;
if(s[i] >= '0' && s[i] <= '9'){
if(as == INT_MAX && bs == INT_MAX) as = i;
else{
if(f == 0) continue;
bs = i;
break;
}
}
}
for(int i = as; i < bs; i ++)
if(s[i] >= '0' && s[i] <= '9')
v += s[i];
for(int i = bs; i < s.length(); i ++)
if(s[i] >= '0' && s[i] <= '9')
w += s[i];
for(int i = 0; i < v.length(); i ++) a[v.length() - i] = v[i] - '0';
for(int i = 0; i < w.length(); i ++) b[w.length() - i] = w[i] - '0';
jia();
// cout << endl;
int len2 = 1;
for(int i = 1 , j = 1; i <= len , j <= len; i ++ , j ++){
if(i >= 5){
int temp = c[i + 1] * 10 + c[i];
if(temp >= sy[j]){
d[len2] += temp % sy[j]; // 1
d[len2 + 1] = temp / sy[j]; // 1
c[i + 1] = d[len2 + 1]; // c[i + 1] == c[i];
len2 ++;
}
}
else{
d[len2] += c[i] % sy[j];
d[len2 + 1] = c[i] / sy[j];
len2 ++;
}
}
for(int i = 1 , j = 1; i <= len2 , j <= len; i ++ , j ++){
if(i >= 5){
int temp = d[i + 1] * 10 + d[i];
if (temp >= sy[j]){
i ++;
d[i + 1] += temp / sy[j];
d[i] = temp % sy[j];
}
}
if(i < 5 && d[i] >= sy[j]){
d[i + 1] += d[i] / sy[j];
d[i] %= sy[j];
}
// cout << d[i] << " " << sy[j] << endl;
}
while(d[len2] == 0 && len2 > 1) len2 --;
for(int i = len2; i >= 1; i --){
if(i != len2){
cout << ",";
}
cout << d[i];
}
return 0;
}
感谢各位大佬!!