博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
除法(暴力)
阅读量:7254 次
发布时间:2019-06-29

本文共 2237 字,大约阅读时间需要 7 分钟。

除法

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 $2 \le N \le 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 

61620

 

Sample Output 

There are no solutions for 61.79546 / 01283 = 6294736 / 01528 = 62 题意: 输入正整数n,按从小到大的顺序输出所有形如abcde/fghij=n的表达式,其中a-j恰好为数字0-9的一个排列(可以有前导0), 。 分析: 1.枚举fghij就可以算出abcde,只需遍历所有的分子即可 2.判断是否所有数字都不相同,数字最大为98765,最小为1234. 3.当abcde和fghij加起来超过10位时可以终止枚举 4.暴力求解 代码:
1 #include
2 #include
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<
100000&&count!=1)46 {47 printf("There are no solutions for %d.\n",n);48 break;49 } 50 }51 }52 return 0;53 }

 

心得:

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

 

 

转载于:https://www.cnblogs.com/ttmj865/p/4684939.html

你可能感兴趣的文章
ATL中对IDocHostUIHandler的封装
查看>>
python - work4
查看>>
MaskedTextBox
查看>>
开源许可协议简介
查看>>
localeCompare() 方法实现中文的拼音排序
查看>>
sqlyog练习
查看>>
Android学习笔记26-图片切换控件ImageSwitcher的使用
查看>>
PHPMailer
查看>>
C# 动态类型与动态编译简介
查看>>
配置DNS服务器
查看>>
C# 2.0学习之--条件编译
查看>>
lock(3)——更新锁(U)、排它锁(X)、死锁及如何避免死锁
查看>>
使用SignalR 2 注意事项
查看>>
多进程 (一) — 像线程一样管理进程
查看>>
node+vue报错合辑
查看>>
Date——js 获取当前日期到之后一个月30天的日期区间
查看>>
RT-SA-2019-003 Cisco RV320 Unauthenticated Configuration Export
查看>>
Java线程练习
查看>>
Algs4-1.5.22Erods-renyi模型的倍率实验
查看>>
计算机硬件的组成、python的开发层面及语法介绍
查看>>