例题7-1 除法 (Division, UVa 725)

题目链接:https://vjudge.net/problem/UVA-725
分类:暴力求解
备注:选择合适的枚举对象

代码如下:

#include<cstdio>
#include<set>
using namespace std;
set<int>tmp;
bool check(int a, int type) {
	if (type)tmp.clear();
	int cnt = 0;
	while (a) {
		if (tmp.count(a % 10))return false;
		tmp.insert(a % 10);
		a /= 10;
		cnt++;
	}
	if (cnt < 4 || (cnt == 4 && tmp.count(0)))return false;
	return true;
}
int main(void) {
	int t, ju = 0;
	while (~scanf("%d", &t) && t) {
		int flag = 0;
		if (ju)printf("\n");
		else ju = 1;
		for (int i = 12345; i <= 98765; i++) 
			if (check(i, 1) && i % t == 0) {
				if (check(i / t, 0)) {
					flag = 1;
					printf("%d / %05d = %d\n", i, i / t, t);
				}
			}
		if (!flag)printf("There are no solutions for %d.\n", t);
	}
	return 0;
}

旧代码如下:

#include<stdio.h>
int  b[10];
int Check(int* ch, int n)
{
	int i, j;
	for (i = 0; i <= n - 2; i++)
	{
		for (j = i + 1; j <= n - 1; j++)
			if (ch[i] == ch[j] && i != j)
				return 1;
	}
	return 0;
}
int main(void)
{
	int x, temp1, temp2, sum, i1, i2, i3, i4, i5, flag = 0;
	scanf("%d", &x);
	while (x != 0)
	{
		sum = 0;
		if (flag)
			printf("\n");
		else
			flag = 1;
		for (i1 = 0; i1 <= 5; i1++)
		{
			b[0] = i1;
			for (i2 = 0; i2 <= 9; i2++)
			{
				b[1] = i2;
				if (Check(b, 2))continue;
				for (i3 = 0; i3 <= 9; i3++)
				{
					b[2] = i3;
					if (Check(b, 3))continue;
					for (i4 = 0; i4 <= 9; i4++)
					{
						b[3] = i4;
						if (Check(b, 4))continue;
						for (i5 = 0; i5 <= 9; i5++)
						{
							b[4] = i5;
							if (Check(b, 5))continue;
							temp1 = (b[4] + 10 * b[3] + 100 * b[2] + 1000 * b[1] + 10000 * b[0]) * x;
							if (temp1 > 98765)break;
							b[5] = temp1 / 10000;
							b[6] = temp1 / 1000 % 10;
							b[7] = temp1 / 100 % 10;
							b[8] = temp1 / 10 % 10;
							b[9] = temp1 % 10;
							if (Check(b, 10))continue;
							else
							{
								sum++;
								printf("%d%d%d%d%d / %d%d%d%d%d = %d\n", b[5], b[6], b[7], b[8], b[9], b[0], b[1], b[2], b[3], b[4], x);
							}
						}
					}
				}
			}
		}
		if (sum == 0)printf("There are no solutions for %d.\n", x);
		scanf("%d", &x);
	}
	return 0;
}
发布了104 篇原创文章 · 获赞 97 · 访问量 4515

猜你喜欢

转载自blog.csdn.net/TK_wang_/article/details/105327801
今日推荐