Multiply
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5042 | Accepted: 2688 |
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.
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 做这道题的时候没做过什么进制转化的题目所以转化的时候要麻烦的转化为十进制再进行转化 这样有点麻烦不过一开始不知道 strtol()函数只能那样转化,这道题还有一点就是没进行__int64的时候不能通过, 所以也改了一会,最后说一下__int64 不支持c++的输入输出流所以以后涉及到它的时候应该用scanf long int strtol(const char *nptr,char **endptr,int base); 这个函数会将参数nptr字符串根据参数base来转换成长整型数。参数base范围从2至36,或0。参数base代表采用的进制方式, 如base值为10则采用10进制, 若base值为16则采用16进制等。当base值为0时则是采用10进制做转换,但遇到如’0x’前置字符则会使用16进制做转换、 遇到’0’前置字符而不是’0x’ 的时候会使用8进制做转换#include<math.h> #include<stdio.h> int bijiao(int x) { int a=0; while(x) { if(x%10>a) a=x%10; x=x/10; } return a; } __int64 multiply(int a,int b,int sum,int c) { int t1=0,t2=0,t3=0; __int64 h1=0,h2=0,h3=0; while(a) { h1+=(a%10* (__int64)pow((double)c,t1));//这里其实是可以优化的用 a=a/10; t1++; } while(b) { h2+=(b%10*(__int64)pow((double)c,t2)); b=b/10; t2++; } while(sum) { h3+=(sum%10*(__int64)pow((double)c,t3)); sum=sum/10; t3++; } return h1*h2-h3; } int main() { int T,p,q,r,i,max; scanf("%d",&T); while(T--) { max=0; scanf("%d%d%d",&p,&q,&r); max=bijiao(p)>bijiao(q)?bijiao(p):bijiao(q); max=bijiao(r)>max?bijiao(r):max; for(i=max+1;i<=16;i++) { if(multiply(p,q,r,i)==0) {printf("%d\n",i); break; } } if(i==17) printf("0\n"); } return 0; }