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)
Source
“高教社杯”第三届福建省大学生程序设计竞赛
Submit Back Status Discuss
本题的大致题意为:给你两个个m进制的数字A和B,让你求出A/B(A,B转化为10进制状态)的值,和A%B的值是多少,然后再输出这两个值。
下面是AC代码:可能代码比较啰嗦,但是比较实在,大家应该可以看得懂。
#include<cstdio>
#include<cstring>
#include<math.h>
#include<algorithm>
using namespace std;
char a[2000],b[2000];
int zpow(int m,int j)
{
int sum=1;
for(int i=0;i<j;i++)
{
sum=sum*m;
}
return sum;
}
int main()
{
int t;
scanf("%d\n",&t);
while(t--)
{
int m;
scanf("%s %s %d",a,b,&m);
int l1=strlen(a);
int l2=strlen(b);
int re1=0,re2=0;
for(int i=l1-1,j=0;i>=0;i--,j++)
{
if(a[i]>='0'&&a[i]<='9')
re1=re1+(a[i]-'0')*zpow(m,j);
else if(a[i]=='a')
{
re1=re1+10*zpow(m,j);
}
else if(a[i]=='b')
{
re1=re1+11*zpow(m,j);
}
else if(a[i]=='c')
{
re1=re1+12*zpow(m,j);
}
else if(a[i]=='d')
{
re1=re1+13*zpow(m,j);
}
else if(a[i]=='e')
{
re1=re1+14*zpow(m,j);
}
else if(a[i]=='f')
{
re1=re1+15*zpow(m,j);
}
}
for(int i=l2-1,j=0;i>=0;i--,j++)
{
if(b[i]>='0'&&b[i]<='9')
re2=re2+(b[i]-'0')*zpow(m,j);
else if(b[i]=='a')
{
re2=re2+10*zpow(m,j);
}
else if(b[i]=='b')
{
re2=re2+11*zpow(m,j);
}
else if(b[i]=='c')
{
re2=re2+12*zpow(m,j);
}
else if(b[i]=='d')
{
re2=re2+13*zpow(m,j);
}
else if(b[i]=='e')
{
re2=re2+14*zpow(m,j);
}
else if(b[i]=='f')
{
re2=re2+15*zpow(m,j);
}
}
int re=re1/re2;
int sum=re1%re2;
printf("(%d,%d)\n",re,sum);
}
return 0;
}