POJ2972(Water)

2972:确定进制

时间限制:

1000ms

内存限制:

65536kB

描述

6*9 = 42 对于十进制来说是错误的,但是对于13进制来说是正确的。即, 6(13) * 9(13) = 42(13), 而 42(13) = 4 * 131 + 2 * 130 = 54(10)。你的任务是写一段程序读入三个整数p、q和 r,然后确定一个进制 B(2<=B<=16) [注意隐性条件,就是B要小于给定的数的各个位数]使得 p * q = r. 如果 B有很多选择, 输出最小的一个。例如: p = 11, q = 11, r = 121. 则有 11(3) * 11(3) = 121(3) 因为 11(3) = 1 * 31 + 1 * 30 = 4(10) 和 121(3) = 1 * 32 + 2 * 31 + 1 * 30 = 16(10)。 对于进制 10,有 11(10) * 11(10) = 121(10)。这种情况下,应该输出 3。如果没有合适的进制,则输出 0。

输入

输入有 T组测试样例。 T在第一行给出。每一组测试样例占一行,包含三个整数p、q、r。 p、q、r的所有位都是数字,并且1 <= p、q、r <= 1,000,000。

输出

对于每个测试样例输出一行。该行包含一个整数:即使得p * q = r成立的最小的B。如果没有合适的B,则输出 0。

样例输入

3

6 9 42

11 11 121

2 2 2

样例输出

13

3

0

 

#include"iostream"
#include"cmath"
using namespace std;
bool check(int pass,int system)
{
 bool ans=true;
 while(pass)
 {
  if(pass>=system)
    ans=false;
  pass/=10;
 }
 return ans;
}
int compute(int pass,int system)
{
 //cout<<"system:"<<system<<endl;
   int temp=0;
   int k=0;
   while(pass)
   {
    temp+=pow((double)system,(double)k)*(pass);
    pass/=10;    
    k++;     
   // cout<<"k:"<<k<<"\t"<<"temp:"<<temp<<endl;
   }
   return temp;
}
int main()
{
 int T;
 cin>>T;
 int a,b,c;
 int result;
 while(T--)
 {
  result=0;
  cin>>a>>b>>c;
  for(int i=2;i<=16;i++)
  {
   if(check(a,i)&&check(b,i)&&check(c,i))
   if(compute(a,i)*compute(b,i)==compute(c,i))
   {
    result=i;
    break;
   }
  }
  cout<<result<<endl;
 }
}

原文地址:https://www.cnblogs.com/lzhitian/p/2140081.html