洛谷-UVA725 除法 Division

在这里插入图片描述
在这里插入图片描述

题意翻译
输入正整数n,从小到大输出所有形如abcde/fghij=n的表达式,其中a-j恰好为数字0-9的一个排列(可以有前导0),2=<n<=79.

输入输出样例
输入 #1 复制
61
62
0
输出 #1 复制
There are no solutions for 61.

79546 / 01283 = 62
94736 / 01528 = 62


思路:只需要枚举fghij就可以算出abcde,然后判断是否所有数字都不相同即可,abcde = n * fghij,从1234开始暴力枚举,当abcde和fghij加起来超过10位时可以终止枚举,然后对的数据进行排序,使用标识,把含有前导零的除数和不含有前导零的除数分开处理。如果符合条件+1统计,输出符合要求的表达式。否则统计结果为0则输出没有解。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
	int n, kase = 0;
	char a[99];
	while(scanf("%d",&n) == 1 && n){
		int cnt = 0;
		if(kase++)
		cout << endl;
		for(int fghij = 1234;; fghij++){//从1234 开始循环暴力搜
		
		int abcde = fghij * n;
		sprintf(a,"%05d%05d",abcde,fghij);
		if(strlen(a) > 10) break; //如果位数长度大于10位 中止
		sort(a, a + 10);//排序
		bool ok = true;
		for(int i = 0 ; i < 10; i++)
		if(a[i] != '0' + i) //不含前导0,标记为 false.
		ok = false;
		if(ok){ 
			cnt++;
			printf("%05d / % 05d = %d\n",abcde,fghij,n);
		}
		
	}
	if(!cnt) printf("There are no solutions for %d.\n", n);
	
 } return 0;
}
发布了430 篇原创文章 · 获赞 2 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/zqhf123/article/details/105381391