UVA725 Division (暴力求解法入门)
uva 725 Division
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
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
bool com(char a, char b)
{
return a>b;
}
char ans[11]="9876543210";
char s[11];
int main()
{
int n, pro, m=0;
bool flag=false;
while(scanf("%d", &n)!=EOF&&n)
{
if(m>0) printf("\n");
m++;//输出技巧
flag=false;
for(int i=1234; i<=50000; i++)
{
pro=n*i;
if(pro>98765) break;
if(i<10000)
sprintf(s, "%d%d%d", 0, i, pro);
else
sprintf(s, "%d%d", i, pro);
sort(s, s+10, com);
if(strcmp(s, ans)==0)
{
printf("%d / %05d = %d\n", pro, i, n);
flag=true;
}
}
if(!flag)
printf("There are no solutions for %d.\n", n);
}
return 0;
}
这题属于入门级暴力求解法。在进行暴力求解枚举时,我们应该进行适当的分析。比如这一题,两个五位数都可以有前导零。我们可以分析一下被除数是不可能前导零的。证明很容易,假设被除数有前导零,说明被除数只是四位数,那么除数必须要五位数,很明显不合题意,因为N>=2;
在进行枚举时,我们可以发现被除数枚举到50000即可(还可以更小,但感觉50000已经优化的可以了)。一旦n*i大于被除数,即可终止for循环。
这是用到的技巧的输出技巧,sprintf,sort;
不得不说,UVA是很严格的,我第一次提交时WA,后来debug是发现我答案多了一行空格。空格处理方法见代码。
我的主要思路是把含有前导零的除数和不含有前导零的除数分开处理。输入到字符数组里,然后sort排序。先定义一个0-9的字符数组(已经排好序列),然后用strcmp比较即可,我感觉我的这种方法比网上的其他代码简洁而且比较好理解。如有不同见解,请在评论中告知。
UVA725 Division (暴力求解法入门)的更多相关文章
- MATLAB线性方程组的迭代求解法
MATLAB线性方程组的迭代求解法 作者:凯鲁嘎吉 - 博客园http://www.cnblogs.com/kailugaji/ 一.实验目的 1. 借助矩阵按模最大特征值,判断解方程组的Jacobi ...
- hdu 4291(矩阵+暴力求循环节)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4291 思路:首先保留求出循环节,然后就是矩阵求幂了. #include<iostream> ...
- Blue Jeans---poj3080(kmp+暴力求子串)
题目链接:http://poj.org/problem?id=3080 题意就是求n个长度为60的串中求最长公共子序列(长度>=3):如果有多个输出字典序最小的: 我们可以暴力求出第一个串的所有 ...
- UVA.725 Division (暴力)
UVA.725 Division (暴力) 题意分析 找出abcdefghij分别是0-9(不得有重复),使得式子abcde/fghij = n. 如果分别枚举每个数字,就会有10^10,肯定爆炸,由 ...
- Java【基础学习】之暴力求素数【用数组返回】
Java[基础学习]之暴力求素数[用数组返回] */ import java.util.*; public class Main{ public static void main(String[] a ...
- UVA725 Division 除法【暴力】
题目链接>>>>>> 题目大意:给你一个数n(2 <= n <= 79),将0-9这十个数字分成两组组成两个5位数a, b(可以包含前导0,如02345 ...
- poj 2187 Beauty Contest (凸包暴力求最远点对+旋转卡壳)
链接:http://poj.org/problem?id=2187 Description Bessie, Farmer John's prize cow, has just won first pl ...
- Poj 2096 (dp求期望 入门)
/ dp求期望的题. 题意:一个软件有s个子系统,会产生n种bug. 某人一天发现一个bug,这个bug属于某种bug,发生在某个子系统中. 求找到所有的n种bug,且每个子系统都找到bug,这样所要 ...
- LightOJ1214 Large Division —— 大数求模
题目链接:https://vjudge.net/problem/LightOJ-1214 1214 - Large Division PDF (English) Statistics Forum ...
随机推荐
- 【mongodb分片中mogos启动的报错】
- springboot+layui实现增删查改
本文描述springboot和layui的结合,采用了springboot内置的jdbc,根据不同需要可以进行修改:分页采用了layui中自带分页格式! ----------------------- ...
- wdcp v3 pureftpd 无法登录问题解决
wdcp v3 新建站点和ftp账号 单位无法登录ftp 在端口中也确实可以看到有进行在登录状态 错误原因: 防火墙端口没有开启该端口范围 20000-30000 这时候发现 改端口为20078 ...
- [Doctrine Migrations] 数据库迁移组件的深入解析三:自定义数据字段类型
自定义type 根据官方文档,新建TinyIntType类,集成Type,并重写getName,getSqlDeclaration,convertToPHPValue,getBindingType等方 ...
- python教程(二)·条件语句
条件语句一般用来判断给定的条件是否成立,根据结果来执行不同的代码,也就是说,有了条件语句,才可以根据不同的情况做不同的事,从而控制程序的流程. 布尔类型 前面说到数据类型的时候,其中有一种叫 &quo ...
- Qt——模态、非模态
模态: 只能操作对话框非模态:要使用 QDialog *_d = new QDialog();_d->setattribute(Qt::WA_DeleteOnClose);_d->show ...
- UART学习之路(二)基本时序介绍
这次我们来介绍一下UART的基本时序,了解一下底层信号怎么传送的.方便以后使用Verilog HDL实现收发逻辑. 9600bit/s 的意思是每秒发送9600bit,因此可以理解为将1s分解为960 ...
- 北京Uber优步司机奖励政策(1月18日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 机器学习常用算法(LDA,CNN,LR)原理简述
1.LDA LDA是一种三层贝叶斯模型,三层分别为:文档层.主题层和词层.该模型基于如下假设:1)整个文档集合中存在k个互相独立的主题:2)每一个主题是词上的多项分布:3)每一个文档由k个主题随机混合 ...
- css 网站常用
简单的loading效果 .progressBar { border: solid 1px #303031; font: bold 20px/22px Arial, sans-serif; backg ...