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 ...
随机推荐
- 使用IdentityServer4实现一个简单的Oauth2客户端模式授权
1.首先新建一个webAPI项目做为IdentityServer的服务端,提供生成Token的服务,首先修改Startup.cs文件,如下图: 2.增加一个Config.cs文件,以便于提供资源和认证 ...
- C# 爬虫 正则、NSoup、HtmlAgilityPack、Jumony四种方式抓取小说
心血来潮,想爬点小说.通过百度选择了个小说网站,随便找了一本小说http://www.23us.so/files/article/html/13/13655/index.html. 1.分析html规 ...
- Tomcat利用MSM实现Session共享方案解说
Session共享有多种解决方法,常用的有四种:1)客户端Cookie保存2)服务器间Session同步3)使用集群管理Session(如MSM) 4)把Session持久化到数据库 针对上面Sess ...
- Linux内核 实践二
实践二 内核模块编译 20135307 张嘉琪 一.实验原理 Linux模块是一些可以作为独立程序来编译的函数和数据类型的集合.之所以提供模块机制,是因为Linux本身是一个单内核.单内核由于所有内容 ...
- oracle alter
ALTER TABLE (表名) ADD CONSTRAINT (索引名);ALTER TABLE (表名) DROP CONSTRAINT (索引名); ALTER TABLE (表名) ADD ( ...
- 文本文件合并(C++实现)
直接附上代码吧 #include<iostream> #include<fstream> #include<cstdlib> using namespace std ...
- 利用ss-redir加速服务器上国外服务的访问
https://blog.microdog.me/2016/06/28/Speed-Up-Network-Accessing-To-Overseas-Services-On-Your-Server/
- WebPage设计专业术语
header footer master content placeholder breadcrumb 面包屑(breadcrumb)源于一个童话,在网站中就是一行层级属性链接组成的线性链接标示(我的 ...
- CSS 范围选择器(自编)
选择第一个到第六个li元素ul li:nth-child(n+3):not(:nth-child(n+6)){} 选择第二个到最后一个ul li:nth-child(2)~li{} 选择除了第一个和最 ...
- FICO基础知识(一)
GL – 总账 (General Ledger) 总帐核算的中心任务是提供外部会计及其所涉及帐户的概貌. 总账会计主要用途:根据不同的会计准则(如欧洲的 IAS, 美国的GAAP, 中国国家会计准则) ...