首先,祝贺大家度过了(单身的)2018年,开启(单身的)2019年。这个辞旧迎新的日子里,就让我们在A题中度过吧。
那么开始看题面:
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 (0) 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
2
6
19
0
Sample Output
10
100100100100100100
111111111111111111
分析:
首先,题目的意思是给你一个数n,让你找出一个由0和1组成的十进制数 m(比如1,10,11,110……),这个十进制m能够被题目所给的这个数n整除。如果这个题目用纯暴力去做 ,非TLE即WA,所以我们选择优雅的暴力(BFS)。如果学会了BFS,我觉得这道题就是个翻译题。
思路:
知道这是道BFS了以后,那么我们要搜寻的就是这个数m。那我们就先从1开始寻找,照完以后把1 * 10和1 * 10+1扔到queue中继续寻找,以此类推,就可以枚举完所有的由0和1组成的10进制数(非常无脑),然后找到能整除的就输出,然后就可以ac了。
代码:
#include<stdio.h>
#include <queue>
#include<math.h>
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e6 + 10;
const int maxm = 1e6 + 10;
const int mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
ll n,mod1;
void bfs(){
queue<ll>p;
while(!p.empty()){
p.pop();
} //这段可要可不要把,不过有些题目没用的话好像就会wa我也不知道为什么
p.push(1);
while(!p.empty()){
ll x=p.front();
p.pop();
if(x%n==0){
printf("%lld\n",x);
break;
}
p.push(x*10);
p.push(x*10+1); //emmmm这段自己想想就理解了
}
}
int main(){
while(~scanf("%lld",&n)&&n!=0){
bfs();
}
} //学业不精告辞
总结:
这是道bfs的水题,因为没用卡你时间,只要无脑去做就好了,当然dfs也可以做,但是会复杂一些。
总之,这道题目教会了我即使样例没过其实还是可以ac的,所以觉得自己没错就大胆的去交题吧(反正错了也就罚时20分钟)。