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 ...
随机推荐
- 【强化学习】python 实现 q-learning 例五(GUI)
本文作者:hhh5460 本文地址:https://www.cnblogs.com/hhh5460/p/10143579.html 感谢pengdali,本文的 class Maze 参考了他的博客, ...
- 1.RapidIO协议概述
转自https://www.cnblogs.com/liujinggang/p/9925859.html 一.RapidIO背景介绍 RapidIO是由Motorola和Mercury等公司率先倡导的 ...
- libgdx学习记录27——线段与线段相交检测
给定p1, p2, p3, p4四个点,p1,p2为一条线段,p3,p4为一条线段,检测其是否有交点. 可分为三种情况: 1. L2与x轴平行 2. L2与y轴平行 3. L2与坐标轴不平行. (L1 ...
- linux中fork, source和exec的区别
转:linux中fork, source和exec的区别 shell的命令可以分为内部命令和外部命令. 内部命令是由特殊的文件格式.def实现的,如cd,ls等.而外部命令是通过系统调用或独立程序实现 ...
- Html5计算MD5值
教程: http://www.tuicool.com/articles/InEBNz 组件: https://github.com/satazor/js-spark-md5
- Centos下部署DRBD+NFS+Keepalived高可用环境记录
使用NFS服务器(比如图片业务),一台为主,一台为备.通常主到备的数据同步是通过rsync来做(可以结合inotify做实时同步).由于NFS服务是存在单点的,出于对业务在线率和数据安全的保障,可以采 ...
- 12.8 Daily Scrum
最近大家都比较忙,任务今天也才刚刚分配,所以具体的编码任务从明天开始. Tomorrow's Task 丁辛 完善餐厅列表,显示距离. 邓亚梅 美化搜索框 ...
- 第七周 linux如何装载和启动一个可执行文件
潘恒 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.实验内容 1.预处理. ...
- Github的建立及心得体会
第一次接触Github,这次注册最大的难处就是全英文,着实看不懂.仅凭着认识的几个常用词去了解个具体内容实在是太困难了.所以第一个体会就是要好好学英语背单词,不想看到满屏的英文就感觉头疼,烦躁.第二个 ...
- 【转】进程同步之信号量机制(pv操作)及三个经典同步问题
原文地址:http://blog.csdn.net/speedme/article/details/17597373 上篇博客中(进程同步之临界区域问题及Peterson算法),我们对临界区,临界资源 ...