Find The Multiple(bfs)
2013-10-21 13:21
330 查看
Find The Multiple
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)Total Submission(s) : 43 Accepted Submission(s) : 21
Special Judge
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2
6
19
0
[align=left]Sample Output[/align]
10
100100100100100100
111111111111111111
[align=left]Source[/align]
PKU
题意:构造一个十进制由0和1组成的整数m,让m能够被n整除;题目中给出了m是小于等于100位的。
思路:bfs可以做出的,只是100位这个条件让我很头疼,我先用string试了试交了之后是超时,后来看了看答案用long long也给过了,所以m肯定没有100位的;
string类型代码:
#include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; queue<long long>s; int a,c; long long b; int bfs() { while(!s.empty()){ b=s.front(); s.pop(); if(b%a==0){ cout<<b<<endl; while(!s.empty()){ s.pop(); } return 0; } else{ s.push(b*10); s.push(b*10+1); } } return 0; } int main() { // freopen("input.txt","r",stdin); while(cin>>a){ if(a==0) break; else{ s.push(1); bfs(); } } return 0; }
View Code
相关文章推荐
- AAA protocol
- asp.net Table控件用法举例
- C# 调用外部程序Process类
- could not change executable permissions on the application
- System V之我见
- [week7]每周总结与工作计划
- TStrings (TStringList)很有功能
- data-theme 几种值的样式
- JAVA日期类型转换
- opendomain 新浪SAE 域名查询
- 为每个页面加上Session判断
- GetClass与RegisterClass的应用一例
- poj: 3253
- AsyncTask和Handler对比
- 【LeetCode OJ】Single Number II
- 完美解决DEDECMS网站中,文章列表页更新点击次数的问题
- PHP mysql_fetch_array() 函数
- ORACLE分析函数-WIDTH-BUCKET
- jsp模板添加URL定位语句
- 关于class produre