SDNU——8题——A

来源:互联网 发布:iphone4s数据恢复 编辑:程序博客网 时间:2024/04/28 07:26

今天终于算是把双十一那天的作业完成了——优美的暴力枚举——贪心算法——数字的排列组合问题

有些算法题哪些绝妙的地方我还真是想不出来,但是在一点一点吃着大佬们的营养代码的时候,我还真的是颇有收获,起码是对某一种算法或者思想有了一些基本的认识和思考~

今天就来总结一下这8道题。

先看第一道

A - 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


心得~~

1.要抽出时间加强一下自己的英文水平,看不懂英文,一切都白搭~~

2.注意输出格式~~~

第一个n值输入后不能有空行,往下的话每输入一个n都要用空行隔开

3.注意暴力的优美(我们都是代码的艺术家)

#include<cstdio>#include<iostream>using namespace std;int main(){int a,b,c,d,e,f,g,h,i,j,flag=0,ret;int n,cnt=0;int w=10000,q=1000,bai=100,s=10;while(1){cin>>n;if(n==0)break;if(cnt++>0)printf("\n");for(f=0;f<=4;f++){for(g=0;g<10;g++){if(f==g)continue;for(h=0;h<10;h++){if(f==h||g==h)continue;for(i=0;i<10;i++){if(f==i||g==i||h==i)continue;for(j=0;j<10;j++){if(f==j||g==j||h==j||i==j)continue;ret=n*(f*w+g*q+h*bai+i*s+j);if(ret>99999)break;e=ret%s;d=(ret/s)%s;c=(ret/bai)%s;b=(ret/q)%s;a=ret/w;if(f!=e&&f!=d&&f!=c&&f!=b&&f!=a&&g!=e&&g!=d&&g!=c&&g!=b&&g!=a&&h!=e&&h!=d&&h!=c&&h!=b&&h!=a){if(i!=e&&i!=d&&i!=c&&i!=b&&i!=a&&j!=e&&j!=d&&j!=c&&j!=b&&j!=a){if(e!=d&&e!=c&&e!=b&&e!=a&&d!=c&&d!=b&&d!=a){if(c!=b&&c!=a&&b!=a){printf("%d%d%d%d%d / %d%d%d%d%d = %d\n",a,b,c,d,e,f,g,h,i,j,n);flag=1;}}}}}}}}}if(flag==0){printf("There are no solutions for %d.\n",n);}else if(flag==1){//printf("\n");flag=0;}}return 0; } 

暴力而不失优美~~~冗长而不超时~~~此乃为优美暴力

为了尽可能的缩短循环的次数根据已知信息,可以推出f的详细循环区间,并且只循环遍历fghij所有可能的值

为了表示出abcde筛选abcde,把原式子变形成abcde=fghij*n

接下来就是耐心的去实现~~判断abcdefghij各不相等

相比那些“壮汉”的“暴力“这个是不是优美许多@cj