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 ...
随机推荐
- python开发的学生管理系统
python开发的学生管理系统(基础版) #定义一个函数,显示可以使用的功能列表给用户 def showInfo(): print("-"*30) print(" 学生管 ...
- foreach传值
- asp.net在一般处理程序里面操作Session
1.要在一般处理程序中获取其他页面的session值,需要引用名空间: using System.Web.SessionState; 2.然后继承一个接口:IRequiresSessionState ...
- Home Assistant系列美化篇——替换天气 UI
替换天气组件 weather 的默认 UI,生成美观大方的气象卡片. Home Assistant 原生的天气平台不少,国内用户常用的有雅虎天气和 Darksky.其他论坛和社区也有分享自制的和风.彩 ...
- Zabbix 3.4.11版本 自定义监控项
一.实验思路过程 创建项目.触发器.图形,验证监控效果: Template OS Linux 模板基本涵盖了所有系统层面的监控,包括了我们最关注的 几项:ping.load.cpu 使用率.memor ...
- 转载:git和github新手安装使用教程(三步入门)
转载防止以后电脑重装,找不到记录. 教程地址:https://www.cnblogs.com/ttjsndx/p/7943444.html
- (数据科学学习手札41)folium基础内容介绍
一.简介 folium是js上著名的地理信息可视化库leaflet.js为Python提供的接口,通过它,我们可以通过在Python端编写代码操纵数据,来调用leaflet的相关功能,基于内建的osm ...
- ACM数论-素数
ACM数论——素数 素数定义: 质数(prime number)又称素数,有无限个.质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数,这样的数称为质数.例 子:2.3.5.7.11.1 ...
- [HDU6315]Naive Operations(线段树+树状数组)
构造一个序列B[i]=-b[i],建一颗线段树,维护区间max, 每次区间加后再询问该区间最大值,如果为0就在树状数组中对应的值+1(该操作可能进行多次) 答案在树状数组中找 其实只用一颗线段树也是可 ...
- print&input--Python
1.print --> 打印到屏幕输出字符串 print("this is a dog!") -->代码 D:\Python\venv\Scripts\python.e ...