Division, UVa 72(暴力求解)
题目链接:https://vjudge.net/problem/UVA-725
Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9 once each, such that the first number divided by the second is equal to an integer N, where 2 ≤ N ≤ 79. That is, abcde fghij = N where each letter represents a different digit. The first digit of one of the 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 terminate the program. Output Your program have to display ALL qualifying pairs of numerals, sorted by increasing 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 must write ‘There are no solutions for N.’. Separate the output for two different 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
题意概括:输入正整数n,按从小到大的顺序输出所有形如abcde/fghij = n的表达式,其中a~j恰好 为数字0~9的一个排列(可以有前导0),2≤n≤79。
解题思路:枚举0~9的所有排列?没这个必要。只需要枚举fghij就可以算出abcde,然后判断是否 所有数字都不相同即可。不仅程序简单,而且枚举量也从10!=3628800降低至不到1万,而且 当abcde和fghij加起来超过10位时可以终止枚举。
第一种方法:
#include<stdio.h>
#include<string.h>
int a[]; //用来存储1-9出现的次数
bool check(int x,int y)
{
memset(a,,sizeof(a));
if(x>) return false;
for(int j=;j<;j++)
{
a[x%]++; a[y%]++;
x/=; y/=;
}
for(int j=;j<=;j++)
{
if(a[j]!=) return false;
}
return true;
}
int main(int argc, char const *argv[])
{
int n,flag,cas=;
while(~scanf("%d",&n)&&n)
{
if(cas++) printf("\n");
flag=;
for(int i=;i<=;i++)
{
if(check(n*i,i))
{
flag=;
printf("%05d / %05d = %d\n",n*i,i,n );
}
}
if(flag) printf("There are no solutions for %d.\n",n);
printf("\n");
}
return ;
}
第二种方法:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int n,kase=;
char buf[];
while(scanf("%d",&n)==&&n)
{
int cnt=;
if(kase++) printf("\n");
for(int fghij=;;fghij++)
{
int abcde=n*fghij;
sprintf(buf,"%05d%05d",abcde,fghij);
int len=strlen(buf);
if(len>) break;
sort(buf,buf+);
bool ok=true;
for(int i=;i<;i++)
{
if(buf[i]!=''+i) 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 ;
}
Division, UVa 72(暴力求解)的更多相关文章
- UVa 725暴力求解
A - Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Su Description Writ ...
- POJ 1562(L - 暴力求解、DFS)
油田问题(L - 暴力求解.DFS) Description The GeoSurvComp geologic survey company is responsible for detecting ...
- 逆向暴力求解 538.D Weird Chess
11.12.2018 逆向暴力求解 538.D Weird Chess New Point: 没有读好题 越界的情况无法判断,所以输出任何一种就可以 所以他给你的样例输出完全是误导 输出还搞错了~ 输 ...
- 隐型马尔科夫模型(HMM)向前算法实例讲解(暴力求解+代码实现)---盒子模型
先来解释一下HMM的向前算法: 前向后向算法是前向算法和后向算法的统称,这两个算法都可以用来求HMM观测序列的概率.我们先来看看前向算法是如何求解这个问题的. 前向算法本质上属于动态规划的算法,也就是 ...
- BestCoder Round #79 (div.2)-jrMz and angles,,暴力求解~
jrMz and angle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- hdu6570Wave (暴力求解)
Problem Description Avin is studying series. A series is called "wave" if the following co ...
- <字符串匹配>KMP算法为何比暴力求解的时间复杂度更低?
str表示文本串,m表示模式串; str[i+j] 和 m[j] 是正在进行匹配的字符; KMP的时间复杂度是O(m+n) , 暴力求解的时间复杂度是O(m*n) KMP利用了B[0:j]和A[i ...
- 暴力求解——除法 Division,UVa 725
Description Write a program that finds and displays all pairs of 5-digit numbers that between them u ...
- 暴力求解——素环数 Prime Ring Problem ,UVa 524
Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers i ...
随机推荐
- TRIO-basic指令--九九乘法表demo
在路上闲的没事,想到之前自己用别的语言实现乘法口诀表,于是来了兴趣用TRIO-basic试一下,挺简单的一段代码,大家看看就好. ' TRIO-basic '实现乘法口诀表 定义两个整型的局部变量 D ...
- zookeeper Error contacting service 解决
连接kafka集群,有一个kafka机器连接失败 到该kafka机器上查询kafka进程,发现没有, 再查看zookeeper状态,提示 Error contacting service. It is ...
- Centos7下关于系统用户密码规则-运维笔记
针对Centos7下的系统用户的密码规则复杂度的设置,处于安全考虑,说明如下: 一.设置密码规则 1)密码长度.有效期 /etc/login.defs文件是当创建用户时的一些规划,比如创建用户时,是否 ...
- Kafka(分布式发布-订阅消息系统)工作流程说明
Kafka系统架构Apache Kafka是分布式发布-订阅消息系统.它最初由LinkedIn公司开发,之后成为Apache项目的一部分.Kafka是一种快速.可扩展的.设计内在就是分布式的,分区的和 ...
- 使用Megacli64对服务器物理磁盘做Raid并通过uuid方式挂载
需求说明:公司最近来了一批服务器,用于大数据业务部署.数据节点服务器由14块物理磁盘,其中有2块是900G的盘,12块是4T的盘.在服务器系统安装时,进入系统的BIOS界面:1)将2块900G的磁盘做 ...
- Spring RPC 入门学习(1)-HelloWorld入门
Spring搭建RPC环境 第一,下载所需要的jar包,下载地址:https://yunpan.cn/cPErQeANrSMyB (提取码:63e5),见下图: 第二,新建动态WebProject,把 ...
- snmpd.conf 配置
开启snmp后,一些指标获取不到,需要配置snmpd.conf文件,如下图所示 参考文章:http://blog.csdn.net/flyingfalcon/article/details/47831 ...
- Sprint 冲刺第三阶段第二天
陈汝婷:播放音乐 1:做播放音乐这个功能时开始没有考虑周全,使用 PS P出来的图竟然没有用上,耗时耗人工.吃一见长一智,以后要考虑周全.还要耗了那么久,音乐的初效果终于出来了. 2:昨天出现的问题, ...
- PAT 1014 福尔摩斯的约会
https://pintia.cn/problem-sets/994805260223102976/problems/994805308755394560 大侦探福尔摩斯接到一张奇怪的字条:“我们约会 ...
- React 模板
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...