UVa 725

  题目大意:给你一个数n(2 <= n <= 79),将0-9这十个数字分成两组组成两个5位数a, b(可以包含前导0,如02345也算),使得a / b = n;列出所有的可能答案。

  暴力枚举,从小到达枚举a,除以n找到b,然后判断a和b是否满足条件。题中要求应该是按a,b以此排序吧(可是也看到有代码先按b排,然后按a排的,不知道这种方法是否正确,没有验证),注意枚举顺序。

 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 bool vis[10];
 5 char str[15];
 6 
 7 bool check(int a, int b)
 8 {
 9     sprintf(str, "%05d%05d", a, b);
10     memset(vis, 0, sizeof(vis));
11     for (int i = 0; i < 10; i++)
12     {
13         if (vis[str[i]-'0'])  return false;
14         vis[str[i]-'0'] = true;
15     }
16     return true;
17 }
18 
19 int main()
20 {
21 #ifdef LOCAL
22     freopen("in", "r", stdin);
23 #endif
24     int n;
25     bool first = true;
26     while (scanf("%d", &n) && n)
27     {
28         bool exist = false;
29         if (first)  first = false;
30         else  printf("
");
31         for (int i = 1234; i <= 98765; i++)
32         {
33             int ans;
34             if (i % n == 0)
35             {
36                 ans = i / n;
37                 if (ans < 1234)  continue;
38                 if (check(i, ans))
39                 {
40                     exist = true;
41                     printf("%05d / %05d = %d
", i, ans, n);
42                 }
43             }
44         }
45         if (!exist)  printf("There are no solutions for %d.
", n);
46     }
47     return 0;
48 }
View Code
原文地址:https://www.cnblogs.com/xiaobaibuhei/p/3301110.html