Multiply
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5179   Accepted: 2773

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

题意:给你三个数字p,q,r,问在哪个最小的进制下p*q=r成立;

注意:假设在k进制下p,q,r的每位上的数字应该要小于k;

#include <iostream>
#include <string.h>
using namespace std;
int change(char *a,int k){
int len=strlen(a);
int ans=0;
for (int i=0;i<len;i++){
ans = ans*k + a[i]-'0';
}
return ans;
}
bool isBig(char *a,int k){
int len=strlen(a);
for (int i=0;i<len;i++){
if (a[i]-'0'>=k)
return false;
}
return true;
}
int main(){
char a[10],b[10],r[10];
int t,i;
cin>>t;
while (t--){
cin>>a>>b>>r;
for (i=2;i<=16;i++){
if (isBig(a,i)==true && isBig(b,i)==true && isBig(r,i)==true){
int aa = change(a,i);
int bb = change(b,i);
int rr = change(r,i);
long long ans = aa*bb;
long long ans2 = rr;
if (ans==ans2)
break;
} }
if (i==17)
i=0;
cout<<i<<endl;
}
return 0;
}

最新文章

  1. C 语言学习:班级同学的博客地址列表
  2. Android 本地/网路下载图片实现放大缩小
  3. js切换不同的div的颜色
  4. 打印文本中的所有单词,并且打印每个单词出现的行号,非实义单词不考虑(TCPL,练习6-3)
  5. 【BZOJ 2693】jzptab
  6. Android Studio 配置
  7. C++仿函数和typename的用法
  8. 【Unity3D基础教程】给初学者看的Unity教程(六):理解Unity的新GUI系统(UGUI)
  9. 05_XML的解析_01_dom4j 解析
  10. The CircuitCalculator.com Blog a blog with live web calculators Home About Policies Contact PCB
  11. jPaginate应用
  12. POJ - 1456 贪心+并查集
  13. vue 统一处理token失效问题
  14. C#子类重写父类函数的两种方法
  15. layui 数据表格+分页+搜索+checkbox+缓存选中项数据
  16. Dubbo的负载均衡
  17. Linux下more命令C语言实现实践 (Unix-Linux编程实践教程)
  18. .Net桌面程序自动更新NAppUpdate
  19. libc.so.6: cannot open shared object file: No such file or diretory
  20. 机器学习之路:python 多项式特征生成PolynomialFeatures 欠拟合与过拟合

热门文章

  1. hanframe开微博了
  2. Angular——配置模块与运行模块
  3. Javascript(IE快捷键操作),ASP技巧
  4. 详谈Struts+Hibernate+Spring三大框架
  5. JMeter在linux上分布式压测遇到的坑(三)
  6. 设置vscode为中文
  7. CAD使用SetxDataDouble写数据(网页版)
  8. CAD删除组(网页版)
  9. &lt;东方梦符祭&gt; N2无尽40波通关
  10. 透彻分析C/C++中memset函数