版权声明:菜鸟一枚~~ 有想法可在下面评论, 转载标明出处即可。 https://blog.csdn.net/KLFTESPACE/article/details/87863596
//这个题没有我写的那么麻烦,我这样写主要是想练习下快速幂和atoi, itoa函数(字符串转十进制整数,十进制整数转n进制字符串)。
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cctype>
using namespace std;
/*快速幂*/
int power(int a, int b) {
int ans = 1, base = a, i = b;
while (b) {
if (b & 1)
ans *= base;
base *= base;
b >>= 1;
}
// printf("%d ^ %d power is %d\n", a, i, ans);
return ans;
}
int main()
{
int n;
char a[15], b[15];
cin >> n;
while(n--){
char c;
int MAXa = 0, MAXb=0;
cin >> a >> b;
getchar();
// cout << a << " " << b << endl;
for(int i = 0; a[i]; i++){
MAXa = (MAXa < a[i]-'0') ? (a[i]-'0') : MAXa;
}
for(int i = 0; b[i]; i++){
// cout << MAXb << " ";
MAXb = (MAXb < b[i]-'0') ? (b[i]-'0') : MAXb;
}
// cout << endl;
int p, q;
p = atoi(a);
q = atoi(b);
// cout<< "p q ::" << p << " " << q << endl;
MAXa += 1;
MAXb += 1;
//刚开始想的是用itoa函数直接将数变为字符串,然后再将字符串用atoi变为数,然而,,这个MAXa进制是由10进制要变成得进制,而不是由MAXa变为10进制。
// itoa(p, a, MAXa);
// itoa(q, b, MAXb);
int a1=0, b1=0, ant=0, bnt=0;
//转换为10进制
while(p){
int tmp = p%10;
a1 += tmp*power(MAXa, ant++);
p /= 10;
}
while(q){
int tmp = q%10;
b1 += tmp*power(MAXb, bnt++);
q /= 10;
}
/* // cout<< "a b ::" << a1 << " " << b1 << endl;
// printf("a:%d b:%d %s %s\n",MAXa, MAXb, a, b);
p = atoi(a);
q = atoi(b);
cout<< "p q ::" << p << " " << q << endl;
*/
cout << a1+b1 << endl;
}
return 0;
}