F Solve equation(FZU 2102)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chimchim04/article/details/78633996

                 F Solve equation


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)


题意分析:
第一行输入一个整数T,接下来有T组数据
每行输入三个数a,b,c;
其中c代表a,b的进制,例如c=3,那a和b就是三进制数。
我们要先把a,b化为10进制数。
然后输出(k,d)中k为a/b,d为a%b。

代码如下:
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        char a[105],b[105];
        int n;
        int x=0,y=0,c=0,d=0;
        scanf("%s%s%d",&a,&b,&n);
        int len1=strlen(a);
        int len2=strlen(b);
        int k=1,l=1;
        for(int i=len1-1; i>=0; i--)   //求a的十进制数
        {
            if(a[i]>='a')
                a[i]=a[i]-'a'+10;
            else
                a[i]=a[i]-'0';
            x+=a[i]*k;
            k*=n;
        }
        for(int i=len2-1; i>=0; i--)    //求b的十进制数
        {
            if(b[i]>='a')
                b[i]=b[i]-'a'+10;
            else
                b[i]=b[i]-'0';
            y+=b[i]*l;
            l*=n;
        }
        c=x/y;
        d=x%y;
        printf("(%d,%d)\n",c,d);
    }
    return 0;
}


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页