Problem A Solve equation
Problem Description
You are given two positive integers A and B in Base C. For the equation:
A=k*B+d
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
For each test case, output the solution “(k,d)” to the equation in Base 10.
Sample Input
3
2bc 33f 16
123 100 10
1 1 2
Sample Output
(0,700)
(1,23)
(1,0)
已知数字转换成任意进制公式 sum=sum*进制+数字
#include <iostream>
#include <cstring>
#include <cmath>
#include <stdio.h>
using namespace std;
int s1[2000];
int s2[2000];
char c1[2000];
char c2[2000];
int main ()
{
int t,C,sum1,sum2,zhengchu,yushu,len1,len2,i;
scanf("%d",&t);
while(t--)
{ sum1=sum2=0;
cin>>c1>>c2>>C;
len1=strlen(c1);
len2=strlen(c2);
for(i=0;i<len1;i++)
{
if(c1[i]>='a'&&c1[i]<='f')
s1[i]=c1[i]-'a'+10;
else
s1[i]=c1[i]-'0';
}
for(i=0;i<len2;i++)
{
if(c2[i]>='a'&&c2[i]<='f')
s2[i]=c2[i]-'a'+10;
else
s2[i]=c2[i]-'0';
}
for(i=0;i<len1;i++)
{
sum1=sum1*C+s1[i];
}
for(i=0;i<len2;i++)
{
sum2=sum2*C+s2[i];
}
zhengchu=sum1/sum2;
yushu=sum1%sum2;
cout<<'('<<zhengchu<<','<<yushu<<')'<<endl;
}
}