例题7-1 UVa725 Division(枚举)

要点:

用0~9编成两个五位数,要求两个数中每个数字只出现一次,第二个数中可以有前导0

要点:

用第二个数直接暴力枚举就可以


#include<stdio.h>
#include<string.h>
int num[10];

int judge(int a,int b)
{
	memset(num, 0, sizeof(num));
	if (b > 98765) return 0;
	if (a < 10000)
		num[0] = 1;
	while (a)
	{
		num[a%10]=1;
		a /= 10;
	}
	while (b)
	{
		num[b % 10]=1;
		b /= 10;
	}
	int sum = 0;
	for (int i = 0; i < 10; i++)
		sum += num[i];
	return (sum == 10);
}
int main()
{
	int n,i,count=0;
	while (scanf("%d", &n), n)
	{
		bool flag = false;
		if (count++)
			printf("\n");
		for (i = 1234; i <50000; i++)
		{
			if (judge(i, n*i))
			{
				printf("%05d / %05d = %d\n", i*n, i, n);//格式要求这中间有空格
				flag = true;
			}
		}
		if (!flag)
			printf("There are no solutions for %d.\n", n);
	}
	return 0;
}


posted @ 2016-03-06 14:45  seasonal  阅读(101)  评论(0编辑  收藏  举报