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 ...
随机推荐
- Docker 小记 — Docker Engine
前言 用了 Docker 方才觉得生产环境终于有了他该有的样子,就像集装箱普及之后大型货轮的价值才逐渐体现出来,Docker 详细说明可查阅"官方文档".本篇为 Docker En ...
- React Native 教程:001 - 如何运行官方控件示例 App
原文发表于我的技术博客 本文主要讲解了如何运行 React Native 官方控件示例 App,包含了一些 React Native 的基础知识以及相关环境的配置. 原文发表于我的技术博客 React ...
- sigar开发(java)
下载sigar,地址:https://yunpan.cn/cBEWbEfdAm98f (提取码:f765) 可以收集的信息 CPU信息:包括基本信息(vendor.model.mhz.cacheSiz ...
- linux第二次读书笔记
<Linux内核设计与实现>读书笔记 第五章 系统调用 第五章系统调用 系统调用是用户进程与内核进行交互的接口.为了保护系统稳定可靠,避免应用程序恣意忘形. 5.1与内核通信 系统调用 ...
- ☆C++学习心得
C++是我进大学的学的第一种编程语言,在高中的时候有电脑课,有教过部分的VB语言,所以其实对编程也并不是非常的陌生,刚开是上课也觉得感觉不难,都懂,没多少课后,恍了个神..居然听不懂了!老师经常让我们 ...
- 第三个sprint冲刺第二阶段
内测版:
- 【Deep Hash】CNNH
[AAAI 2014] Supervised Hashing via Image Representation Learning [paper] [code] Rongkai Xia , Yan Pa ...
- HDOJ4548_美素数
简单的素数问题 HDOJ4548_美素数 #include<stdio.h> #include<stdlib.h> #include<math.h> #includ ...
- 10-Python3从入门到实战—基础之函数
Python从入门到实战系列--目录 函数的定义 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数的语法 def 函数名(参数列表): 函数体 函数代码块以 def 关键词开头 ...
- JQuery Cross Domain Ajax(jsonp)
http://www.pureexample.com/jquery/cross-domain-ajax.html http://www.pureexample.com/ExampleTesterII- ...