C - Find The Multiple (BFS)题解
【还是题目优先】
咕噜咕噜一大圈:
The long-lost Sunday is coming again, and the ACM Laboratory Elimination Competition of Beihua University has begun. Now you have to complete a task: XLS is very happy, because IG won. Now to make him happy, just ask you to help him solve this problem, give you an n, let you find a multiple of M and m for n, but M has a limit. His number is only 0 or 1\. Can you help him solve this problem?
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
Each input you export a corresponding M, m does not know the unique number of m does not exceed 200 bits.
然后我帮你完完整整翻译了一遍:
失传已久的星期天又将来临,北华大学ACM实验室淘汰大赛也开始了。现在你必须完成一个任务:
XLS非常高兴,因为IG赢了。为了让他开心,就让你帮他解决这个问题:
(这里开始)给你一个n,让你找一个M和m的倍数,但是M有一个限定。他的号码只有0或1。你能帮他解决这个问题吗?输入文件可能包含多个测试用例。每一行包含一个值n(1<=n<=200)。包含零的行结束输入。输出每个输出对应的M,m不知道m的唯一数目不超过200位。
也就是说,找一个数M是n的倍数,而且M只有数字0,1构成;
【解析】
这BFS的非地图应用,刚开始我还真没看懂,不过后来也想到了,所以说,地图上没有宝藏 (ಡωಡ)(认真脸)
就是0,1存数字就行啦!!!注意由于是数字不超200位,尽量还是用long long,再上升难度可结合超大数字运算,但是本题没那么难,仅考察BFS。
【代码来了】
1 #include<cstdio> 2 #include<queue> 3 using namespace std; 4 typedef long long ll; 5 void bfs(int n) 6 { 7 ll start=1,b; 8 queue <ll> a; 9 a.push(start); 10 while(!a.empty()) 11 { 12 b=a.front(); 13 a.pop(); 14 if(b%n==0) 15 { 16 printf("%lld\n",b); 17 return; 18 } 19 a.push(b*10); 20 a.push(b*10+1); 21 } 22 return; 23 } 24 int main() 25 { 26 int n; 27 while(~scanf("%d",&n)&&n) 28 bfs(n); 29 return 0; 30 }

浙公网安备 33010602011771号