FZU2102Solve equation

Problem 2102 Solve equation

Accept: 881    Submit: 2065
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 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)

      题目意思很好懂吧,然而做的时候就卡在了进制转换这,特意去百度了一下怎么转10进制;

     网上是这样给的:

          假如一个数abcdef,是x进制数,转10进制就是a*x^5+b*x^4+c*x^3+d*x^2+e*x^1+f*x^0;看懂了吧,当时还真这样用for循环遍历了一遍,还真对,运行结果及其他测试样例也都没错,但这思路代码活生生CE了6遍;

         CE:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<ctype.h>
using namespace std;
int main()
{
    int t,a,b,c,x1,x2;
    char aa[55],bb[55];
    scanf("%d",&t);
    while(t--)
    {
        memset(aa,'0',sizeof(aa));
           memset(bb,'0',sizeof(bb));
        a=b=0;
        scanf("%s%s%d",aa,bb,&c);
         x1=strlen(aa);
         x2=strlen(bb);
         int x11=x1,x22=x2;
         if(c==10)
         {
             for(int i=0;i<x1;i++)
             a=a*10+(aa[i]-'0');
             for(int i=0;i<x2;i++)
             b=b*10+(bb[i]-'0');
         }
         else
         {
             for(int i=0;i<x1;i++)
         {
             if(islower(aa[i]))
                a+=(aa[i]-'a'+10)*pow(c,x11-i-1);
                else
             a+=(aa[i]-'0')*(pow(c,(x11-i-1)));
         }
           for(int i=0;i<x2;i++)
         {
             if(islower(bb[i]))
                  b+=(bb[i]-'0'+10)*(pow(c,(x22-i-1)));//记得pow好像适用于double,可能要用pow(double(c),_);
                  else
             b+=(bb[i]-'0')*(pow(c,(x22-i-1)));
         }
         }
       int k=a/b,d=a-k*b;
       printf("(%d,%d)\n",k,d);
    }
    return 0;
}

     就这样浪费了一个水题;



             看以AC的代码发现他们都是这样转10进制的: 字符串a输入,假如长度x,是c进制数,那么转10进制   int aa=0;

                (1)      for(i=0;i<x;i++)

                               aa=aa*10+(aa[i]-'0')//字符串本身代表的就是10进制数;

         (2)    

                 for(i=0;i<x;i++)

                        aa=aa*10+(aa[i]-'a'+10)//字符串本身代表的不是10进制数,,,,,百度上怎么没有,,亿脸懵逼;;;


                  AC:

                           

<span style="font-size:18px;">#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<ctype.h>
using namespace std;
int main()
{
    int t,a,b,c,x1,x2;
    char aa[55],bb[55];
    scanf("%d",&t);
    while(t--)
    {
        a=b=0;
        scanf("%s%s%d",aa,bb,&c);
        x1=strlen(aa);
        x2=strlen(bb);
            for(int i=0; i<x1; i++)
            {
                if(islower(aa[i]))
                    a=a*c+(aa[i]-'a')+10;
                else
                    a=a*c+(aa[i]-'0');
            }
            for(int i=0; i<x2; i++)
            {
                if(islower(bb[i]))
                   b=b*c+(bb[i]-'a')+10;
                else
                    b=b*c+(bb[i]-'0');
            }
        int k=a/b,d=a-k*b;
        printf("(%d,%d)\n",k,d);
    }
    return 0;
}</span>

                      

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值