注:本人英语很渣,题目大意大多来自百度~=0=

 
这个题有点坑,答案不唯一
 
题目大意:给你一个数n, 你需要输出的是一个由1和0组成的数,此数能被n整除
 
解题思路:用s = 1做数的起点, s*10则相当于在后面加上0, s*10+1代表在后面加1, 用long long 来保存s足够了, 每次判断一下s % n  符合条件则输出
当然用dfs不能无限寻找下去  比如你第一次10000...0 假如n是奇数不可能有结果  所以每次递归记录一下层数;
long long 的范围是-9223 37203 68547 75808~9223 37203 68547 75807  所以查到19层如果还找不到结果的话, 就结束递归
 
下面是代码
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. #include <cstdlib>
  6. #include <cmath>
  7. #include <cctype>
  8. #define N 20010
  9. #define each(i,n) (int i=1;i<=(n);++i)
  10. using namespace std;
  11. int a;
  12. int dfs(long long s, int c)//c用来记录层数
  13. {
  14. if(c == 19) return 0;
  15. if(s % a == 0) {
  16. printf("%lld\n", s);
  17. return 1;
  18. }
  19. else {
  20. int b = dfs(s * 10, c + 1);
  21. if(!b)
  22. dfs(s * 10 + 1, c + 1);
  23. }
  24. }
  25. int main()
  26. {
  27. while(scanf("%d", &a), a) {
  28. dfs(1, 0);
  29. }
  30. }

  

POJ 1426 Find The Multiple的更多相关文章

  1. 广搜+打表 POJ 1426 Find The Multiple

    POJ 1426   Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25734   Ac ...

  2. POJ 1426 Find The Multiple --- BFS || DFS

    POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...

  3. POJ 1426 Find The Multiple(寻找倍数)

    POJ 1426 Find The Multiple(寻找倍数) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Given ...

  4. POJ.1426 Find The Multiple (BFS)

    POJ.1426 Find The Multiple (BFS) 题意分析 给出一个数字n,求出一个由01组成的十进制数,并且是n的倍数. 思路就是从1开始,枚举下一位,因为下一位只能是0或1,故这个 ...

  5. DFS/BFS(同余模) POJ 1426 Find The Multiple

    题目传送门 /* 题意:找出一个0和1组成的数字能整除n DFS:200的范围内不会爆long long,DFS水过~ */ /************************************ ...

  6. POJ 1426 Find The Multiple (DFS / BFS)

    题目链接:id=1426">Find The Multiple 解析:直接从前往后搜.设当前数为k用long long保存,则下一个数不是k*10就是k*10+1 AC代码: /* D ...

  7. POJ 1426 Find The Multiple(数论——中国同余定理)

    题目链接: http://poj.org/problem?id=1426 Description Given a positive integer n, write a program to find ...

  8. POJ 1426 - Find The Multiple - [DP][BFS]

    题目链接:http://poj.org/problem?id=1426 Given a positive integer n, write a program to find out a nonzer ...

  9. poj 1426 Find The Multiple( bfs )

    题目:http://poj.org/problem?id=1426 题意:输入一个数,输出这个数的整数 倍,且只有0和1组成 程序里写错了一个数,结果一直MLE.…… #include <ios ...

  10. POJ - 1426 Find The Multiple(搜索+数论)

    转载自:優YoU  http://user.qzone.qq.com/289065406/blog/1303946967 以下内容属于以上这位dalao http://poj.org/problem? ...

随机推荐

  1. Nginx与Lua利用fpm打成rpm包

    1.下载相关软件 需要软件有:Nginx,LuaJIT,ngx_devel_kit,ngx_lua等安装文件 安装Lua或者LuaJIT都是可以的,但是出于效率的考虑,推荐安装LuaJITshell& ...

  2. boost库的使用

    我的经验:在强调运行效率的场合:shared_ptr/weak_ptr很好用,尤其是weak_ptr这个能够判断一个对象的生命周期的玩意,可以简化和扩展很多设计.unordered系列的容器也很不错. ...

  3. 模板——Tarjan

    #include <cstdio> #include <cstring> #include <iostream> #include <vector> u ...

  4. tableau 连接R语言

    如何开始使用 Tableau 与 R? 对于已经熟悉 R 及其功能的用户而言,在 R 与 Tableau 之间建立连接非常简单.以 下说明适用于基于开源版 R 的新安装.其他一些方案也可能使用其他程序 ...

  5. phpcms 中路径问题

    <table width="100%" border="0" cellspacing="0" cellpadding="0& ...

  6. QAbstractItemView::setRootIndex(const QModelIndex & index) 失效

    问题: 在逻辑中使用了, QAbstractItemView::setRootIndex(const QModelIndex & index), 第一次设置生效, view 进入了model ...

  7. 用SignalR实现的共享画板例子

    使用HTML5的canvas画布功能,在页面进行绘画,然后通过SignalR将画布的每个点的颜色提交到服务端,服务端同时将这些画布的信息推送到其他客户端,实现共享同一个画板的功能 类似下图,在某一个浏 ...

  8. java文章显示内容部分(将html转成纯文本)

    public static String splitAndFilterString(String input, int length) { if (input == null || input.tri ...

  9. windows无法访问指定设备路径或文件。您可能没有合适的权限访问

    试试退出杀毒软件试试,被杀毒软件阻拦了.

  10. webpack和gulp的区别

    Gulp应该和Grunt比较,他们的区别我就不说了,说说用处吧.Gulp / Grunt 是一种工具,能够优化前端工作流程.比如自动刷新页面.combo.压缩css.js.编译less等等.简单来说, ...