UVA.725 Division (暴力)

题意分析

找出abcdefghij分别是0-9(不得有重复),使得式子abcde/fghij = n。

如果分别枚举每个数字,就会有10^10,肯定爆炸,由于分数值已知,其实发现可以通过枚举分母,来计算出分子,然后再看看这些数字是否符合题意即可。

在枚举分母的时候,也可以根据条件过滤掉很多数字,我这里没有优化,直接暴力扫描1234-99999。

#include <iostream>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#define nmax 200
#define MEM(x) memset(x,0,sizeof(x))
using namespace std;
int digit[10];
int n;
int getdigit(int num)
{
int cnt = 0;
while(num){
cnt++;
num/=10;
}
return cnt;
}
bool caldigit(int num)
{
if(getdigit(num)<=4) digit[0]++;
else if(getdigit(num) >=6) return false;
while(num){
int t = num%10;
digit[t] ++;
num/=10;
}
for(int i = 0; i<=9; ++i){
if(digit[i] == 0) return false;
else if(digit[i] == 2) return false;
}
return true;
}
bool caldigiti(int num)
{
memset(digit,0,sizeof(digit));
if(getdigit(num)<=4) digit[0]++;
else if(getdigit(num) >=6) return false;
while(num){
int t = num%10;
digit[t] ++;
num/=10;
}
int cnt = 0;
for(int i = 0; i<=9;++i){
if(digit[i] == 2) return false;
else if(digit[i] == 1) cnt++;
}
if(cnt == 5) return true;
else return false;
}
void output(int a, int b)
{
int len1 = getdigit(a),len2 = getdigit(b);
for(int i = 5-len1; i>0 ; --i) printf("0");
printf("%d / ",a);
for(int i = 5-len2;i>0 ;--i) printf("0");
printf("%d = %d\n",b,n);
}
int main()
{
//freopen("in.txt","r",stdin);
bool isfirst = true;
while(scanf("%d",&n) && n){
if(!isfirst) printf("\n");
int sta = 1234;bool isok = false;
for(int i = sta;i<=99999;++i){
if(caldigiti(i)){
int ans = n*i;
if(caldigit(ans)){
output(ans,i);
isok = true;
}
}
}
if(!isok) printf("There are no solutions for %d.\n",n);
if(isfirst) isfirst = false; }
return 0;
}

UVA.725 Division (暴力)的更多相关文章

  1. uva 725 DIVISION (暴力枚举)

    我的56MS #include <cstdio> #include <iostream> #include <string> #include <cstrin ...

  2. 暴力枚举 UVA 725 Division

    题目传送门 /* 暴力:对于每一个数都判断,是否数字全都使用过一遍 */ #include <cstdio> #include <iostream> #include < ...

  3. uva 725 Division(除法)暴力法!

    uva 725  Division(除法) A - 暴力求解 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & ...

  4. uva 725 Division(暴力模拟)

    Division 紫书入门级别的暴力,可我还是写了好长时间 = = [题目链接]uva 725 [题目类型]化简暴力 &题解: 首先要看懂题意,他的意思也就是0~9都只出现一遍,在这2个5位数 ...

  5. UVA 725 division【暴力枚举】

    [题意]:输入正整数n,用0~9这10个数字不重复组成两个五位数abcde和fghij,使得abcde/fghij的商为n,按顺序输出所有结果.如果没有找到则输出“There are no solut ...

  6. UVa 725 Division (枚举)

    题意 : 输入正整数n,按从小到大的顺序输出所有形如abcde/fghij = n的表达式,其中a-j恰好为数字0-9的一个排列(可以有前导0),2≤n≤79. 分析 : 最暴力的方法莫过于采用数组存 ...

  7. Uva 725 Division

    0.不要傻傻的用递归去构造出一个五位数来,直接for循环最小到最大就好,可以稍微剪枝一丢丢,因为最小的数是01234 从1234开始,因为倍数n最小为2 而分子是一个最多五位数,所以分母应该小于五万. ...

  8. uva 725 division(水题)——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABVMAAAOHCAIAAAClwESxAAAgAElEQVR4nOydybGturJFcQEPfgQu4A

  9. UVA 725 – Division

    Description   Write a program that finds and displays all pairs of 5-digit numbers that between them ...

随机推荐

  1. dva 路由跳转

    1.从props取出并传递history 取 const { history } = this.props 用 <button onClick={ () => history.push(' ...

  2. 「日常温习」Hungary算法解决二分图相关问题

    前言 二分图的重点在于建模.以下的题目大家可以清晰的看出来这一点.代码相似度很高,但是思路基本上是各不相同. 题目 HDU 1179 Ollivanders: Makers of Fine Wands ...

  3. MySQL日期函数、时间函数总结(MySQL 5.X)

    一.获得当前日期时间函数 1.1 获得当前日期+时间(date + time)函数:now() select now(); # :: 除了 now() 函数能获得当前的日期时间外,MySQL 中还有下 ...

  4. git学习笔记(一)——从已存在的远程仓库克隆

    应用场景:在公司电脑把脚本上传到公司的gitlab上,在家里想继续写: 问题: 家里的之前代码连的是github的仓库,需要把公钥替换成公司gitlab的. 环境:win10,pycharm,git ...

  5. Selenium(Python) ddt读取CSV文件数据驱动

    import csvimport unittestfrom time import sleep from ddt import ddt, data, unpackfrom selenium impor ...

  6. Python 多线程、进程、协程上手体验

    浅谈 Python 多线程.进程.协程上手体验 前言:浅谈 Python 很多人都认为 Python 的多线程是垃圾(GIL 说这锅甩不掉啊~):本章节主要给你体验下 Python 的两个库 Thre ...

  7. python常用命令—windows终端查看安装包信息

    1, pip list 会将 Python 的所有安装包全部显示出来, 左边是包名, 右边是包的版本号. 2, pip show 包的名字 会将这个包的名字,版本号,包的功能说明,按装这个包的路径显示 ...

  8. Map Reduce Application(Top 10 IDs base on their value)

    Top 10 IDs base on their value First , we need to set the reduce to 1. For each map task, it is not ...

  9. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

  10. LINUX监控一:监控命令

    简单的整理一下常用的linux监控命令 本篇参考了:http://www.cnblogs.com/JemBai/archive/2010/07/30/1788484.html的内容 1.top top ...