题意:给定三个数a,b,c(仅含数字,进制未知)。问满足a*b=c的最小进制。已知进制范围为[2,16]。
思路:先确定起始的可能进制(a,b,c所含所有数字最大的加1),然后将其转化为10进制测试。
#include <stdio.h> #include <string.h> #define max(a,b) ((a)>(b)?(a):(b)) #define TYP __int64 int a,b,c,T; TYP convert(int x,int b){ TYP res=0; TYP temp = 1; while(x){ res += x%10*temp; x /= 10; temp *= b; } return res; } int maxd(int x){ int res=0; while(x){ res = max(res,x%10); x /= 10; } return res; } int test(int a,int b,int c,int base){ TYP x,y,z; x = convert(a,base); y = convert(b,base); z = convert(c,base); if(x*y == z) return 1; return 0; } int main(){ freopen("a.txt","r",stdin); scanf("%d",&T); while(T--){ int i=0; scanf("%d %d %d",&a,&b,&c); i=max(i,maxd(a)); i=max(i,maxd(b)); i=max(i,maxd(c)); for(++i;i<=16;i++) if(test(a,b,c,i)) break; if(i>16) printf("0\n"); else printf("%d\n",i); } }