You are given two positive integers A and B in Base C. For the equation:
We know there always existing many non-negative pairs (k, d) that satisfy the equation above. Now in this problem, we want to maximize k.
For example, A="123" and B="100", C=10. So both A and B are in Base 10. Then we have:
(1) A=0*B+123
(2) A=1*B+23
As we want to maximize k, we finally get one solution: (1, 23)
The range of C is between 2 and 16, and we use 'a', 'b', 'c', 'd', 'e', 'f' to represent 10, 11, 12, 13, 14, 15, respectively.
Input
The first line of the input contains an integer T (T≤10), indicating the number of test cases.
Then T cases, for any case, only 3 positive integers A, B and C (2≤C≤16) in a single line. You can assume that in Base 10, both A and B is less than 2^31.
Output
Sample Input
Sample Output
题意:进制之间的转化;
#include <cstdio>
#include <iostream>
#include <cctype>
#include <cmath>
#include <cstring>
using namespace std;
string a,b;
int base;
int main () {
int n;
scanf("%d",&n);
while (n--) {
cin >>a;
cin >> b;
scanf("%d",&base);
int len1 = a.length();
int len2 = b.length();
int sum1 = 0, sum2 = 0;
for (int i = 0; i < len1; i++) {
if (isdigit(a[i])) a[i] = a[i] - '0';
else a[i] = a[i] - 'a' + 10;
sum1 += a[i]*pow(double(base),len1-i-1);
}
for (int i = 0; i < len2; i++){
if (isdigit(b[i])) b[i] = b[i] -'0';
else b[i] = b[i] -'a'+10;
sum2 += b[i]*pow(double(base),len2-i-1);
}
printf("(%d,%d)\n",sum1/sum2,sum1%sum2);
}
return 0;
}