除法(暴力)

除法

Description

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 . 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), 。

分析:1.枚举fghij就可以算出abcde,只需遍历所有的分子即可2.判断是否所有数字都不相同,数字最大为98765,最小为1234.3.当abcde和fghij加起来超过10位时可以终止枚举4.暴力求解

代码:
 1 #include<cstdio>
 2 #include<iostream>
 3 using namespace std;
 4
 5 bool tese(int i,int j)    //用数组a存放i,j的各位数字
 6 {
 7     int a[10]={0};        //初始化数组a,使得各位数字为0,使fghij<10000 时f位置为0
 8     int t=0;
 9     while(i)              //取i中各位数字存放在数组a中
10     {
11         a[t++]=i%10;
12         i=i/10;
13     }
14     while(j)              //取j中各位数字存放在数组a中
15     {
16         a[t++]=j%10;
17         j=j/10;
18     }
19     //判断a~j是否恰好为数字的0~9的一个排列
20     for(int m=0;m<=10;++m)
21         for(int n=m+1;n<10;++n)
22             if(a[n]==a[m])
23                 return 0;
24             return 1;
25 }
26 int main()
27 {
28     int n,k,s=0;
29     while(scanf("%d",&n)&&n>=2&&n<=79)
30     {
31         if(s++)          //输出一行空行
32             cout<<endl;
33         int count=0;
34         for(k=1234;k<=98765;k++)
35         {
36             int l=k*n;
37             if(l<100000)
38             {
39                 if(tese(l,k))
40                 {
41                     printf("%05d / %05d = %d\n",l,k,n);
42                    count=1;
43                 }
44             }
45             else if(l>100000&&count!=1)
46             {
47                 printf("There are no solutions for %d.\n",n);
48                 break;
49             }
50         }
51     }
52     return 0;
53 }

心得:

以前只是听过用暴力方法解题的,不知道暴力是什么,现在真正用到暴力,发现暴力确实很简单。继续暴力吧。

				
时间: 2024-08-24 20:21:59

除法(暴力)的相关文章

2015湖南省省赛 阶乘除法 暴力

阶乘除法 Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: NBUT 1643 Description 输入两个正整数 n, m,输出 n!/m!,其中阶乘定义为 n!= 1*2*3*...*n (n>=1). 比如,若 n=6, m=3,则 n!/m!=6!/3!=720/6=120. 是不是很简单?现在让我们把问题反过来:输入 k=n!/m!,找到这样的整数二元组(n,m) (n>m>=1). 如果答案不唯

c++20701除法(刘汝佳1、2册第七章,暴搜解决)

20701除法 难度级别: B: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述     输入正整数n,按从小到大的顺序输出所有满足表达式abcde/fghij=n的abcde和fghij,其中a~j恰好为数字0~9的一个排列. 如果没有符合题意的数,输出0.本题所说的五位数可以包括前导0的,如01234在这里也称为五位数. 输入 一个正整数n  输出 若干行,每行包括两个符合要求的五位正整数(每行的两个数先大后小),两数之

UVALive 6163(暴力枚举)

这道题我的做法就是枚举这四个数的所有排列所有运算所有计算顺序. 略有考验代码能力,不能漏掉情况,注意模块化的思想,一些功能写成函数调试的时候结构清晰好分析. 比赛时没有AC是对next_permutation()函数理解的不透,根本没有想到是没有从最小字典序开始枚举的问题. 就是next_permutation()函数是从当前顺序枚举到字典序最大的,而我开始时do里面的a数组不一定是字典序最小的,但是next_permutation()函数一定是从当前字典序往最大的枚举,所以漏掉了字典序很小的那

暴力穷举

暴力除法 题目描述 输入正整数n,按从小到大的顺序输出所有形如abcde/fghij=n的表达式,其中a~j恰好为数字0~9的一个排列,2<=n<=79 输入 输入包括多行,每行一个正整数n,2<=n<=79 输出 针对每个输入的n,从小到大输出该表达式,若没有表达式可以生成,则不输出. 样例输入 62 样例输出 79546/01283=62 94736/01528=62 #include<iostream> #include<string.h> using

uva 725 Division(除法)暴力法!

uva 725  Division(除法) A - 暴力求解 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description 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 t

线段树维护区间开方/除法

今天考试考了一些神仙数据结构 T1 线段树维护区间加,区间开方,区间和 (数据范围:5e5) T2 线段树维护区间加,区间除,区间和,区间最值 对于这些题目,就像是之前考的区间与,区间或一样,除法,开方的操作会让各个数字之间越来越相近,最后变成一串一串连续的数字都是一样的,所以对于这一部分的操作我们一定程度上使用暴力,而如果一段都相等就相当于直接进行区间剪发的操作 那么我们来看如何判断区间一段都相等,那我们只需要维护区间的最值,最小值==最大值就完全相等了 然后.....代码.....被 \(y

Link/Cut Tree CodeForces - 614A 暴力+爆 long long 处理

题意: 给你一个区间[l,r],让你从小到大输出k^x,设y=k^x,要保证y在区间[l,r]中 题解: 就算k是最小的2也不需要枚举多少次就到long long的极限了,所以暴力没商量,根本不会TLE 然后就是爆long long处理,比如r特别大,当k^x=<r但是k^(x+1)就爆long long了,这个要注意一下 代码: 1 #include<stdio.h> 2 #include<string.h> 3 #include<iostream> 4 #in

二叉树的后序遍历(暴力版) 小白菜oj 1034

给出二叉树的前序遍历和中序遍历,求二叉树的后序遍历-- 作为一个搜索蒟蒻,我真的没有办法很和谐的A掉,但估计过几天就会写有关这个题的和谐的解法--但只是估计-- 下面讲述我的超暴力解法-- 首先,先由前序遍历得到一个父亲节点,然后再由中序遍历得到这个父亲节点的左子树和右子树中的元素(中序遍历中,该点的左边的所有点,都在它的左子树,右边的都在它的右子树,子树中的根节点是在这些节点的前序遍历中排名最靠前的),然后递归建树,之后在递归求后序遍历即可. 但这个方法有两个比较--&¥--&的问题:1

shell中除法运算

A=`expr $num1 / $num2` 这个时候num3=0 ,是因为是因为expr不支持浮点除法 小数点标识的方法: A=`echo "scale=2; $num1/$num2" | bc` 使用bc工具,sclae控制小数点后保留几位 另一种方法 A=awk 'BEGIN{printf "%.2f\n",'$num1'/'$num2'}' 百分比表示 A=awk 'BEGIN{printf "%.2f%\n",('$num1'/'$nu