FZU-2102-Solve equation

A - Solve equation
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

FZU 2102
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)

一道简单的进制转换问题,这个oj上要自定义pow函数才能A,还有就是要读懂题意啊!!!!
代码

#include <iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<string>
#include<string.h>
using namespace std;
//进制转换
char s1[1005];//接受A
char s2[1005];//接受B
int A,B,C;//如题
int pow(int c,int k)//自定义c的k次方
{
    int i,ans = 1;
    for(i=1; i<=k; i++)
        ans*=c;
    return ans;
}
int main()
{
    int T;//T组数据
    scanf("%d",&T);
    while(T--)
    {
        memset(s1,'\0',sizeof(s1));
        memset(s2,'\0',sizeof(s2));
        scanf("%s%s%d",s1,s2,&C);
        A=0;//s1转化为十进制的A
        int ans=0;//进制的ans次方
        int length=strlen(s1);
        for(int i=length-1; i>=0; i--)
        {
            if(s1[i]>='0'&&s1[i]<='9')
                A=A+(s1[i]-'0')*pow(C,ans);
            else
                A=A+(s1[i]-'a'+10)*pow(C,ans);
            ans++;
        }
        B=0;//s2转化为十进制的B
        ans=0;//进制的ans次方
        length=strlen(s2);
        for(int i=length-1; i>=0; i--)
        {
            if(s2[i]>='0'&&s2[i]<='9')
                B=B+(s2[i]-'0')*pow(C,ans);
            else
                B=B+(s2[i]-'a'+10)*pow(C,ans);
            ans++;
        }
        int K=A/B;
        int D=A-K*B;
        printf("(%d,%d)\n",K,D);
    }
    return 0;
}

不知不觉又过了零点。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值