uva 725 DIVISION (暴力枚举)

我的56MS

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <string>
 4 #include <cstring>
 5 #include <queue>
 6 #include <vector>
 7 #include <map>
 8 #include <cmath>
 9 using namespace std;
10 
11 #define MEM(a,v) memset (a,v,sizeof(a))
12 // a for address, v for value
13 
14 #define max(x,y) ((x)>(y)?(x):(y))
15 #define max(x,y) ((x)>(y)?(x):(y))
16 
17 #define debug printf("!
")
18 
19 int num[15];
20 bool visited[15];
21 
22 bool check(int a,int b)
23 {
24           MEM(visited,false);
25 
26           int j;
27 
28           for(j = 5;j>=1;j--)
29           {
30                     int tmp = a%10;
31                     num[j] = tmp;
32                     a/=10;
33           }
34           for(j = 10;j>=6;j--)
35           {
36                     int tmp = b%10;
37                     num[j] = tmp;
38                     b/=10;
39           }
40 
41           for(j = 1;j<=10;j++)
42           {
43                     if(visited[num[j]])
44                               return false;
45                     visited[num[j]] = true;
46           }
47           return true;
48 
49 
50 }
51 
52 int main()
53 {
54           int i,n,j,k,T=0;
55           while(~scanf("%d",&n) && n)
56           {
57                     if(T++)
58                               printf("
");
59                     bool find = false;
60                     for(i = 1234;i<=98765;i++)
61                     {
62                               if(i%n==0)
63                               {
64                                         int m = i/n;
65                                         if(check(i,m))
66                                         {
67                                                   find =true;
68                                         for(j = 1;j<=5;j++)
69                                                   printf("%d",num[j]);
70                                         printf(" / ");
71                                         for(j = 6;j<=10;j++)
72                                                   printf("%d",num[j]);
73                                         printf(" = %d
",n);
74                                         }
75                               }
76                     }
77                     if(!find)
78                               printf("There are no solutions for %d.
",n);
79           }
80 
81           return 0;
82 }

但要特别提一下这位仁兄写的,有狠多值得学习的地方

http://blog.csdn.net/mobius_strip/article/details/38735773

#include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;

int used[10];

int judge( int a, int b )
{
    //对于判断是否有重复的数字,可以用一个used数组
    //将用过的项置为1
    
    if ( b > 98765 ) return 0;
    for ( int i = 0 ; i < 10 ; ++ i )
        used[i] = 0;
    if ( a < 10000 ) used[0] = 1;
    while ( a ) {
        used[a%10] = 1;
        a /= 10;
    }
    while ( b ) {
        used[b%10] = 1;
        b /= 10;
    }
    int sum = 0;
    for ( int i = 0 ; i < 10 ; ++ i )
        sum += used[i];
    return (sum == 10);
}

int main()
{
    int n, T = 0;
    while ( ~scanf("%d",&n) && n ) {
        if ( T ++ ) printf("
");
        int count = 0;
        for ( int i = 1234 ; i < 100000 ; ++ i ) {
            if ( judge( i, i*n ) ) { 
                printf("%05d / %05d = %d
",i*n,i,n);
                //可以用%05d填充,就不用数组来保存数字了
                count ++;
            }
        }
        if ( !count ) 
            printf("There are no solutions for %d.
",n);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/qlky/p/5020068.html