【基础训练-进制转换】Multiply

Description
6*9 = 42" is not true for base 10, but is true for base 13. That is, 6(13) * 9(13) = 42(13) because 42(13) = 4 * 131 + 2 * 130 = 54(10). 

You are to write a program which inputs three integers p, q, and r and determines the base B (2<=B<=16) for which p * q = r. If there are many candidates for B, output the smallest one. For example, let p = 11, q = 11, and r = 121. Then we have 11(3) * 11(3) = 121(3) because 11(3) = 1 * 31 + 1 * 30 = 4(10) and 121(3) = 1 * 32 + 2 * 31 + 1 * 30 = 16(10). For another base such as 10, we also have 11(10) * 11(10) = 121(10). In this case, your program should output 3 which is the smallest base. If there is no candidate for B, output 0. 
Input
The input consists of T test cases. The number of test cases (T ) is given in the first line of the input file. Each test case consists of three integers p, q, and r in a line. All digits of p, q, and r are numeric digits and 1<=p,q, r<=1,000,000.
Output
Print exactly one line for each test case. The line should contain one integer which is the smallest base for which p * q = r. If there is no such base, your program should output 0.
Sample Input
3
6 9 42
11 11 121
2 2 2
Sample Output
13
3
0

Source


问题链接 POJ1331 UVALive2526 Multiply
问题简述 :(略)
问题分析
   这是一个简单的进制转换问题
  输入的数据都是有数字(0-9)组成的。
  由于如果有两个答案的话则输出小的值,所以需要从小到大试探进制。
  开始时,先将数据按照10进制读入,然后再转换为指定的进制。
程序说明
   编写了共用的函数convert()用于转换数据。  

题记
   将共用功能用 封装 到函数中是一种好的做法。

参考链接 :(略)

AC的C语言程序如下:
[cpp]   view plain   copy
  1. /* POJ1331 UVALive2526 Multiply */  
  2.   
  3. #include <stdio.h>  
  4.   
  5. #define BASE10 10  
  6. #define START 2  
  7. #define END 16  
  8.   
  9. int convert(int val, int base)  
  10. {  
  11.     int ans, weight, r;  
  12.   
  13.     ans = 0;  
  14.     weight = 1;  
  15.     while(val) {  
  16.         r = val % BASE10;  
  17.         val /= BASE10;  
  18.   
  19.         if(r >= base)  
  20.             return -1;  
  21.   
  22.         ans += weight * r;  
  23.   
  24.         weight *= base;  
  25.     }  
  26.   
  27.     return ans;  
  28. }  
  29.   
  30. int main(void)  
  31. {  
  32.     int t, p, q, r, i;  
  33.     int p2, q2, r2;  
  34.   
  35.     scanf("%d", &t);  
  36.     while(t--) {  
  37.         scanf("%d%d%d", &p, &q, &r);  
  38.   
  39.         for(i=START; i<=END; i++) {  
  40.             p2 = convert(p, i);  
  41.             if(p2 < 0)  
  42.                 continue;  
  43.   
  44.             q2 = convert(q, i);  
  45.             if(q2 < 0)  
  46.                 continue;  
  47.   
  48.             r2 = convert(r, i);  
  49.             if(r2 < 0)  
  50.                 continue;  
  51.   
  52.             if(p2 * q2 == r2)  
  53.                 break;  
  54.         }  
  55.   
  56.         printf("%d\n", i <= END ? i : 0);  
  57.     }  
  58.   
  59.     return 0;  
  60. }  
学到的点:
①一个固定样子的数,尝试它为任意进制,从而得到不同值(十进制)。
②枚举法。continue用得好。注意最后的i还要再判断是否<=END, 因为有可能不是break出来的,不合要求。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值