暴力枚举 UVA 725 Division

本文提供了一种解决UVA725编程题的方法,通过判断两个整数的组合是否包含从1到5的所有数字各一次来寻找解。代码采用C++实现,并通过实例验证了算法的有效性。

 

题目传送门

 1 /*
 2     暴力:对于每一个数都判断,是否数字全都使用过一遍
 3 */
 4 #include <cstdio>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <cmath>
 8 #include <cstring>
 9 #include <string>
10 #include <map>
11 #include <set>
12 #include <queue>
13 using namespace std;
14 
15 const int MAXN  = 1e4 + 10;
16 const int INF = 0x3f3f3f3f;
17 int vis[10];
18 
19 bool ok(int x, int y)
20 {
21     memset (vis, 0, sizeof (vis));
22     for (int i=1; i<=5; ++i)
23     {
24         vis[x%10]++;    vis[y%10]++;
25         if (vis[x%10] > 1 || vis[y%10] > 1)    return false;
26         x /= 10;    y /= 10;
27     }
28 
29     return true;
30 }
31 
32 int main(void)        //UVA 725 Division
33 {
34     //freopen ("UVA_725.in", "r", stdin);
35 
36     int n, cnt = 0;
37     while (scanf ("%d", &n) == 1)
38     {
39         if (n == 0)    break;
40         if (cnt++)    puts ("");
41 
42         int one = 0;
43         for (int i=1234; i<=100000/n; ++i)
44         {
45             if (i * n > 98765)    break;
46             if (ok (i, i*n) == true)
47             {
48                 printf ("%05d / %05d = %d\n", n*i, i, n);    one++;
49             }
50         }
51 
52         if (!one)    printf ("There are no solutions for %d.\n", n);
53     }
54 
55     return 0;
56 }
57 
58 
59 /*
60 There are no solutions for 61.
61 */

 

转载于:https://www.cnblogs.com/Running-Time/p/4463227.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值