Uva 725 Division



Division 

Write a program that finds and displays all pairs of 5-digit numbers thatbetween them use the digits0 through 9 once each, such that thefirst number divided by the second is equal to an integerN, where $2\le N \le 79$.That is,


abcde / fghij = N

where each letter represents a different digit. The first digit of one ofthe numerals is allowed to be zero.

Input 

Each line of the input file consists of a valid integer N. An input of zero is to terminatethe program.

Output 

Your program have to display ALL qualifying pairs of numerals, sorted byincreasing numerator (and, of course, denominator).

Your output should be in the following general form:


xxxxx / xxxxx = N

xxxxx / xxxxx = N

.

.


In case there are no pairs of numerals satisfying the condition, you mustwrite ``There are no solutions forN.". Separate the output for twodifferent values of N by a blank line.

Sample Input 

61
62
0

Sample Output 

There are no solutions for 61.

79546 / 01283 = 62
94736 / 01528 = 62



Miguel Revilla
2000-08-31




问题描述:


1、输入正整数n,按abcde /fghij = n的形式输出所有商等于n的算式,若不存在则按要求输出特殊信息,其中a—j0—9的不重复的正整数,前导0不省略,2<=n<=79.


2、输入以0为结束,输出每组结果之间空一行,结尾不含空行(就是说只要1\n即可)。


思路:


简单的暴力枚举,注意输出格式,注意缩小不必要的枚举范围。


个人看法:


本人acm刚入门,代码很糟糕,求原谅。。。做的时候直接想枚举十位数,想想不太现实,可以利用只枚举fghij利用已知的nabcde,去掉重复数字和超范围的就行了,严重注意输入输出格式。


代码:

#include<stdio.h>
#include<math.h>
bool is_r(int n,int n1,int a,int b,int c,int d,int e)
{
	int a1, b1, c1, d1, e1;
	a1 = n1 / 10000;
	b1 = n1 % 10000 / 1000;
	c1 = n1 % 1000 / 100;
	d1 = n1 % 100 / 10;
	e1 = n1 % 10;
	if (a1 == b1 || a1 == c1 || a1 == d1 || a1 == e1) return false;
	if (b1 == c1 || b1 == d1 || b1 == e1)	return false;
	if (c1 == d1 || c1 == e1)	return false;
	if (d1 == e1)	return false;
	if (a1 == b || a1 == c || a1 == d || a1 == e || a1==a) return false;
	if (b1 == a || b1 == b || b1 == c || b1 == d || b1==e) return false;
	if (c1 == a || c1 == b || c1 == c || c1 == d || c1==e) return false;
	if (d1 == a || d1 == b || d1 == c || d1 == d || d1 == e) return false;
	if (e1 == a || e1 == b || e1 == c || e1 == d || e1 == e) return false;
	printf("%d%d%d%d%d / %d%d%d%d%d = %d\n",a1,b1,c1,d1,e1,a,b,c,d,e,n);
	return true;

}
int main()
{
	//freopen("uva725.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	int n;
	int n1 = 0, n2 = 0;
	int r = 0;
	while (scanf("%d", &n) != EOF&&n != 0)
	{
		if (r) printf("\n");
		r++;
		int flag = 0;
		for (int a = 0; a <= 9; a++)
			for (int b = 0; b <= 9; b++)
			{	
				if (b == a) continue;
				for (int c = 0; c <= 9; c++)
				{
					if (c == a || c == b) continue;
					for (int d = 0; d <= 9; d++)
					{
						if (d == a || d == b || d == c)	continue;
						for (int e = 0; e <= 9; e++)
						{
							if (e == a || e == b || e == c || e == d) continue;
							n2 = a * 10000 + b * 1000 + c * 100 + d * 10 + e;
							n1 = n2*n;
							if (n1 >= 99999) continue;
							if(is_r(n,n1,a,b,c,d,e)) flag=1;
						}
					}
				}
			}
		if (flag == 0)
			printf("There are no solutions for %d.\n",n);
		
	}
	return 0;
}


参考书籍:《算法竞赛入门经典(第二版)》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值