POJ-1426.Findthemultiple.(BFS)

一开始模拟了一波大数取余结果超时了,最后改成long long过了emmm...  

  本题大意:给出一个200以内的数n,让你找出一个m使得m % n == 0,要求m只有1和0组成。

  本题思路:BFS模拟即可。

  参考代码:

 1 #include <cstdio>
 2 #include <queue>
 3 using namespace std;
 4 
 5 int mod;
 6 typedef long long LL;
 7 
 8 LL bfs() {
 9     LL now = 1;
10     queue <LL> s;
11     s.push(now);
12     while(!s.empty()) {
13         LL cur = s.front();
14         s.pop();
15         for(int i = 0; i < 2; i ++) {
16             LL next;
17             if(i == 1) {
18                 next = cur * 10 + 1;
19             } else next = cur * 10;
20             if(next % mod == 0)  return next;
21             s.push(next);
22         }
23     }
24     return 0;
25 }
26 
27 int main () {
28     while(~scanf("%d", &mod) && mod) {
29         LL ans = bfs();
30         printf("%lld\n", ans);
31     }
32     return 0;
33 }
View Code

  顺便给出模拟大数取余的代码。

 1 #include <string>
 2 #include <iostream>
 3 using namespace std;
 4 
 5 int main () {
 6     int mod;
 7     string s;
 8     while(cin >> mod >> s) {
 9         int ans = 0;
10         for(int i = 0; i < s.length(); i ++)
11             ans = (ans * 10 + s[i] - '0') % mod;
12         cout << ans << endl;
13     }
14     return 0;
15 }

 

转载于:https://www.cnblogs.com/bianjunting/p/10482981.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值