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
问题简述:(略)
问题分析:
这是一个简单的进制转换问题。
输入的数据都是有数字(0-9)组成的。
由于如果有两个答案的话则输出小的值,所以需要从小到大试探进制。
开始时,先将数据按照10进制读入,然后再转换为指定的进制。
程序说明:
编写了共用的函数convert()用于转换数据。
题记:
将共用功能用封装到函数中是一种好的做法。
参考链接:(略)
AC的C语言程序如下:
/* POJ1331 UVALive2526 Multiply */ #include#define BASE10 10 #define START 2 #define END 16 int convert(int val, int base) { int ans, weight, r; ans = 0; weight = 1; while(val) { r = val % BASE10; val /= BASE10; if(r >= base) return -1; ans += weight * r; weight *= base; } return ans; } int main(void) { int t, p, q, r, i; int p2, q2, r2; scanf("%d", &t); while(t--) { scanf("%d%d%d", &p, &q, &r); for(i=START; i<=END; i++) { p2 = convert(p, i); if(p2 < 0) continue; q2 = convert(q, i); if(q2 < 0) continue; r2 = convert(r, i); if(r2 < 0) continue; if(p2 * q2 == r2) break; } printf("%d\n", i <= END ? i : 0); } return 0; }