• 百练 2972 确定进制


    链接:http://poj.grids.cn/practice/2972/题目:

    总时间限制:
    1000ms
    内存限制:
    65536kB
    描述
    6*9 = 42 对于十进制来说是错误的,但是对于13进制来说是正确的。即, 6(13) * 9(13) = 42(13), 而 42(13) = 4 * 131 + 2 * 130 = 54(10)。 你的任务是写一段程序读入三个整数p、q和 r,然后确定一个进制 B(2<=B<=16) 使得 p * q = r. 如果 B有很多选择, 输出最小的一个。例如: p = 11, q = 11, r = 121. 则有 11(3) * 11(3) = 121(3) 因为 11(3) = 1 * 31 + 1 * 30 = 4(10) 和 121(3) = 1 * 32 + 2 * 31 + 1 * 30 = 16(10)。 对于进制 10,有 11(10) * 11(10) = 121(10)。这种情况下,应该输出 3。如果没有合适的进制,则输出 0。
    输入
    输入有 T组测试样例。 T在第一行给出。每一组测试样例占一行,包含三个整数p、q、r。 p、q、r的所有位都是数字,并且1 <= p、q、r <= 1,000,000。
    输出
    对于每个测试样例输出一行。该行包含一个整数:即使得p * q = r成立的最小的B。如果没有合适的B,则输出 0。
    样例输入
    3
    6 9 42
    11 11 121
    2 2 2
    样例输出
    13
    3
    0
    

    代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 using namespace std;
     6 
     7 const int MAX = 8;
     8 const int LEFT = 2;
     9 const int RIGHT = 16;
    10 
    11 int main()
    12 {
    13     //freopen("F:\\input.txt","r",stdin);
    14     
    15     int t;
    16     cin>>t;
    17     
    18     char p[MAX];
    19     char q[MAX];
    20     char r[MAX];
    21     
    22     while(t--)
    23     {
    24         scanf("%s %s %s",p,q,r);
    25         
    26         
    27         int i,j;
    28         int p10,q10,r10; 
    29         
    30         for(i = LEFT; i <= RIGHT; i++)
    31         {
    32             //i进制装成10进制
    33             
    34             //p
    35             p10 = 0;
    36             int len_p = strlen(p);
    37             for(j = 0; j < len_p; j++)
    38             {
    39                 p10 = p10 * i + (p[j] - '0');
    40                 if(p[j] - '0' >= i) break;
    41             }
    42             if(j < len_p) continue;
    43             
    44             //q
    45             q10 = 0;
    46             int len_q = strlen(q);
    47             for(j = 0; j < len_q; j++)
    48             {
    49                 q10 = q10 * i + (q[j] - '0');
    50                 if(q[j] - '0' >= i) break;
    51             }
    52             if(j < len_q) continue;
    53             
    54             //r
    55             r10 = 0;
    56             int len_r = strlen(r);
    57             for(j = 0; j < len_r; j++)
    58             {
    59                 r10 = r10 * i + (r[j] - '0');
    60                 if(r[j] - '0' >= i) break;
    61             }
    62             if(j < len_r) continue;
    63             
    64             if( p10 * q10 == r10) break;
    65             //cout<<p10<<','<<q10<<','<<r10<<endl;
    66         }
    67         
    68         if(i <= RIGHT)
    69         {
    70             cout<<i<<endl;
    71         }
    72         else
    73         {
    74             cout<<'0'<<endl;
    75         }
    76     }
    77     return 0;
    78 }

    思路

    1.读入信息,使用字符串式读入

    2.转成10进制,注意如:121不可能为2进制,因为2不可能出现在2进制数中

  • 相关阅读:
    jquery_ajax 地址三级联动
    delphi窗体按钮灰化禁用
    sqlserver查找断号,回收单据号
    query.locate过个过滤参数
    StringGrid换行功能
    你不知道的JavaScript--Item13 理解 prototype, getPrototypeOf 和__proto__
    你不知道的JavaScript--Item12 undefined 与 null
    你不知道的JavaScript--Item11 arguments对象
    你不知道的JavaScript--Item10 闭包(closure)
    你不知道的JavaScript--Item9 call(),apply(),bind()与回调
  • 原文地址:https://www.cnblogs.com/mobileliker/p/3095905.html
Copyright © 2020-2023  润新知