uva-343 进制转换

#include
#include
#include

#define MAX_L 128

int find_min(char *s)
{
	char cc='0';
	while(*s)
	{
		if(*s>cc)
			cc=*s++;
		else
			s++;
	}
	if(cc=='0')
		return 2;
	else if(cc<='9')
		return cc-'0'+1;
	else
		return cc-'A'+11;
}

int conv(char *s, int base)
{
	int len=strlen(s), w=1, sum=0;
	while(len-->0)
	{
		if(s[len]>='A')
			sum+=w*(s[len]-'A'+10);
		else
			sum+=w*(s[len]-'0');
		w*=base;
	}
	return sum;
}

int main(void)
{
	char n1[MAX_L], n2[MAX_L];
	int i, j, m1, m2, b1, b2, flag=0;

#ifndef ONLINE_JUDGE
	freopen("p.in", "r", stdin);
#endif
	while(scanf("%s%s", n1, n2)!=EOF)
	{
		flag=0;
		m1=find_min(n1);
		m2=find_min(n2);
		for(i=m1; i<=36 && !flag; i++)
			for(j=m2; j<=36 && !flag; j++)
				if(conv(n1, b1=i) == conv(n2, b2=j))
					flag=1;
		if(!flag)
			printf("%s is not equal to %s in any base 2..36\n", n1, n2);
		else
			printf("%s (base %d) = %s (base %d)\n", n1, b1, n2, b2);
	}
	return 0;
}


你可能感兴趣的:(uva-343 进制转换)