ZOJ 1136 Mutiple (同余定理判重)

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <queue>
 6 #define sc(x) scanf("%d",&x)
 7 #define pf(x) printf("%d\n",x)
 8 #define P printf("\n")
 9 #define CL(x,y) memset(x, y, sizeof(x))
10 #define FOR(i,x,v) for(int i=x; i<=v; i++)
11 using namespace std;
12 struct node
13 {
14     int num;
15     string str;
16 };
17 const int MAX = 5002;
18 int N, M, arr[10], used[MAX], i, temp;
19 queue <node> Q;
20 void BFS();
21 int main()
22 {
23     int i;
24     while(sc(N)!=EOF)
25     {
26         sc(M);
27         FOR(i, 0, M-1)
28         sc(arr[i]);
29         sort(arr, arr+M);
30         CL(used, 0);
31         while(!Q.empty ())
32             Q.pop ();
33         if(N==0) pf(0);
34         else
35             BFS();
36         while(!Q.empty())
37             Q.pop ();
38 //        P;
39     }
40     return 0;
41 }
42 void BFS()
43 {
44     node fir, cur, next;
45     fir.num = 0;
46     fir.str = "";
47     Q.push(fir);
48     while(!Q.empty())
49     {
50         cur = Q.front();
51         Q.pop();
52 //        if(cur.num >= N && cur.num % N == 0)
53 //        {
54 //            pf(cur.num);
55 //            return ;
56 //        }
57         FOR(i, 0, M-1)
58         {
59             temp = cur.num*10 + arr[i];
60             if(!temp) continue;
61             if(temp%N==0)
62             {
63                 cout << cur.str << (char)(arr[i]+'0') << endl;
64                 return ;
65             }
66             if(!used[temp%N])
67             {
68                 used[temp%N] = 1;
69                 next.num = temp%N;
70                 next.str = cur.str + (char)(arr[i]+'0');
71                 Q.push(next);
72             }
73         }
74     }
75     pf(0);
76 }
View Code

虽然错了10次,还是不清楚到底怎么了。。。等几天才懂了那个string 的含义

转载于:https://www.cnblogs.com/ghostTao/p/4336004.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值