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. 「日常训练」Known Notation(ZOJ-3829)

    题意与分析 题意是这样的:给一个字符串,字符串中只包含数字和运算符'*'.现在问字符串是不是一个合法的逆波兰式(后缀表达式).已知逆波兰式的空格消除,也就是说123可以看成123也可以看成1和23.如 ...

  2. 前端开发工程师 - 01.页面制作 - 第4章.CSS

    第4章.CSS CSS简介 Cascading Style Sheet 层叠样式表:定义页面中的表现样式 history: CSS1(1996)--CSS2(1998)--着手CSS3草案(拆分成很多 ...

  3. HDU - 6440(费马小定理)

    链接:HDU - 6440 题意:重新定义加法和乘法,使得 (m+n)^p = m^p + n^p 成立,p是素数.,且satisfied that there exists an integer q ...

  4. leetcode合并区间

    合并区间     给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: ...

  5. Git版本库工作流程图想

    对照廖雪峰的教程,发现有很多难以理解的地方,画了一个图想方便以后参考 首先两个基本命令反应了版本库最本质的工作流程,后面的命令其实都基于此git add 把文件修改添加到暂存区git commit 在 ...

  6. Wordcount -- MapReduce example -- Mapper

    Mapper maps input key/value pairs into intermediate key/value pairs. E.g. Input: (docID, doc) Output ...

  7. Python3 小工具-UDP扫描

    from scapy.all import * import optparse import threading def scan(target,port): pkt=IP(dst=target)/U ...

  8. [leetcode-784-Letter Case Permutation]

    Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...

  9. 基于angular+bower+glup的webapp

    一:bower介绍 1:全局安装安装bower cnpm i -g bower bower常用指令: bower init //初始化文件 bower install bower uninstall ...

  10. C++ STL victor

    一.介绍 vector是表示可变大小数组的序列容器. 就像数组一样,vector也采用的连续存储空间来存储元素.也就是意味着可以采用下标对vector的元素进行访问,和数组一样高效.但是又不像数组,它 ...