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 ...
随机推荐
- Intel x86_64 Architecture Background 3
多层次的cache结构解决了CPU和DRAM之间处理速度不一致的问题,在Intel体系架构下,CPU核心和主存DRAM之间有着三层的cache.其中一层缓存L1和二层缓存L2在CPU核心(core)中 ...
- 阿里云Centos搭建jdk环境
当我们开始了自己的开发,那么云服务器是一定少不了的,当然也有很多同学只是在本地做开发研究. 这里记录一下我自己在阿里云上搭建环境的过程. 趁着优惠的时候,我在阿里云上购买了ECS云服务器,并且搭载了C ...
- centos7.4下Jira6环境部署及破解操作记录(完整版)
废话不多说,以下记录了Centos7针对Jira6的安装,汉化,破解的操作过程,作为运维笔记留存. 0) 基础环境 192.168.10.212 Centos7.4 mysql 5.6 jdk 1.8 ...
- Nextcloud私有云盘在Centos7下的部署笔记
搭建个人云存储一般会想到ownCloud,堪称是自建云存储服务的经典.而Nextcloud是ownCloud原开发团队打造的号称是“下一代”存储.初一看觉得“口气”不小,刚推出来就重新“定义”了Clo ...
- sigar开发(java)
下载sigar,地址:https://yunpan.cn/cBEWbEfdAm98f (提取码:f765) 可以收集的信息 CPU信息:包括基本信息(vendor.model.mhz.cacheSiz ...
- c#词频统计命令行程序
这里将用c#写一个关于词频统计的命令行程序. 预计时间分配:输入处理3h.词条排序打印2h.测试3h. 实际时间分配:输入处理1h.词条排序打印2h.测试3h.程序改进优化6h. 下面将讲解程序的完成 ...
- 个人博客作业Week7(阅读文章,心得体会)
Alpha阶段结束了,内心可以说是五味杂陈.不是说我们的产品拿不上台面那般差劲,复杂的心绪主要来源于和别的队的比较,别的队才刚刚发布没多久访问量和注册量就破百了,并且还发起了找bug送红包的活动.可能 ...
- 【ML】ICML2015_Unsupervised Learning of Video Representations using LSTMs
Unsupervised Learning of Video Representations using LSTMs Note here: it's a learning notes on new L ...
- [BUAA软工]第1次阅读
[BUAA软工]第1次阅读 本次作业所属课程: 2019BUAA软件工程 本次作业要求: 第1次个人作业 我在本课程的目标 熟悉和实践软件工程流程,适应团队开发 本次作业的帮助 帮助理解<构建之 ...
- 实训十二(stick的设定)
上篇我们介绍到人物主角的设定,其实人物是有工具使的,那就是——stick小棍. 信息的获取.起始位置.长度的加载.边界的判断.位置.长度重置是需要我们主要考虑的问题 信息获取上考虑的使什么时候加载st ...