poj 1331 Multiply,poj1331multiply

來源:互聯網
上載者:User

poj 1331 Multiply,poj1331multiply

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

36 9 4211 11 1212 2 2

Sample Output

1330

題意:給你三個數字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;}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.