博客园 首页 私信博主 显示目录 隐藏目录 管理

除法

题意:输入正整数n,用0~9这10个数字不重复组成两个五位数abcde和fghij,使得abcde/fghij的商为n,按顺序输出所有结果。

如果没有找到则输出“There are no solutions for N.”。这里2<=n<=79。

样例输入: 62

样例输出:

79546/01238=62

94736/01528=62

暴力也要注意技巧啊 。。。

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 const int maxn=10;
 5 
 6 int check(int abcde,int fghij){
 7     int a[maxn]={0};
 8     if(fghij<10000) a[0]=1;
 9 
10     while(abcde){
11         int d=abcde%10;
12         if(a[d]) return 0;
13         a[d]=1;
14         abcde/=10;
15     }
16     while(fghij){
17         int f=fghij%10;
18         if(a[f]) return 0;
19         a[f]=1;
20         fghij/=10;
21     }
22     return 1;
23 }
24 
25 int main(){
26     int n;
27     cin>>n;
28     int end=98765/n;
29     int cnt=0;
30     for( int i=1234; i<=end; i++ ){
31         int k=i*n;
32         if(k>=12345&&check(k,i)){
33             printf("%05d / %05d = %d\n",k,i,n);
34         }
35         cnt++;
36     }
37     if(cnt==0){
38         printf("There are no solutions for %d.\n",n);
39     }
40     return 0;
41 }

 

posted @ 2019-02-01 20:21  Brave_WTZ  阅读(238)  评论(0编辑  收藏  举报