POJ1426Find The Multiple
来源:互联网 发布:手机c语言编程 编辑:程序博客网 时间:2024/06/05 14:29
Description
Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits.
Input
The input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input.
Output
For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.
Sample Input
26190
Sample Output
10100100100100100100111111111111111111
题目大意:求n的倍数,但倍数的十进制数都是由0和1构成且不超过一百位。
方法:n小于200,bfs和dfs都可以,但是dfs注意要控制下边界。看了比人的博客,感觉有点高深,我还是这么水过去了吧。
dfs:
#include<iostream>#include<climits>#include<cstring>using namespace std;unsigned long long a;bool found;void dfs(unsigned long long tem, int level){ if(found || level >18) return ; if(tem % a == 0) { found = true; cout << tem <<endl; return; } dfs(tem*10, level+1); if(found) return ; dfs(tem*10+1, level+1);}int main(){ while(cin >> a && a) { found = false; dfs(1, 0); } return 0;}
#include<iostream>#include<climits>#include<queue>using namespace std;long long bfs(long long a){ queue<long long> q; q.push(1); while(!q.empty()) { long long tem = q.front(); q.pop(); if(tem % a == 0) return tem; q.push(tem*10); q.push(tem*10 + 1); } return 10000000;}int main(){ long long b, ans[201]; for(long long int i = 1; i <= 200; i++) ans[i] = bfs(i); while(cin >> b && b) { cout << ans[b] << endl; } return 0;}
0 0
- poj1426Find The Multiple(AC)
- poj1426Find The Multiple(bfs)
- POJ1426Find The Multiple
- poj1426Find The Multiple
- poj1426Find The Multiple(bfs ohohoh ~~~~好害怕)
- POJ1426 Find The Multiple
- POJ_1426_Find The Multiple
- poj1426 Find The Multiple
- G - Find The Multiple
- poj1426 - Find The Multiple
- POJ1426 Find The Multiple
- POJ1426-Find The Multiple
- Find The Multiple
- POJ_1426Find The Multiple
- poj find the multiple
- poj1426 Find The Multiple
- poj1426-Find The Multiple
- POJ1426 Find the multiple
- 变量及变量类型
- vps的使用方法及介绍
- jqery中无法通过 .data(key,value)方法来设置 data-*的属性
- Android控件颜色的透明度设定
- js window.onload意思跟jquery的$(function() { } )的区别
- POJ1426Find The Multiple
- cgroup之cpuset基础
- PHP正则替换函数preg_replace和preg_replace_callback使用总结
- USB通信相关
- Android消息传递之组件间传递消息
- java发送邮件
- mysql调优
- Java算法
- css之align 与 valign 的用法比较