POJ 1426 Find The Multiple
Find The Multiple
Time Limit: 1000MS | Memory Limit: 10000K | |||
Total Submissions: 21433 | Accepted: 8774 | Special Judge |
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
2 6 19 0
Sample Output
10 100100100100100100 111111111111111111
这个题本身是一个水题的,但是与Runtime Error斗争了好久。。。
虽然这个题好像可以包含到100个0与1,但是实际上在20个1与0内就可以找到满足条件的余200之内的数为0的数。
所以我们只需要用一个unsigned long long就可以解决问题,简单的一个dfs可以解决。
但是真正在做的时候,我一开始将n设置为全局变量,每次的结果都是Runtime Error,仔细检查了也找不到原因,看了看网上的解题报告,也和自己的代码差不太多。但是发现他们都是将n设置为局部变量,抱着试一试的态度,竟然AC了,我还是不太明白为什么这会影响到提交的结果。望有高手指点
贴上代码:
/************************************************************************* > File Name: Find_the_Multiple.cpp > Author: Zhanghaoran0 > Mail: chiluamnxi@gmail.com > Created Time: 2015年07月27日 星期一 00时43分12秒 ************************************************************************/ #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; long long int key; bool flag; void dfs(unsigned long long x, int step, int n){ if(flag) return ; if(x % n == 0){ cout << x << endl; flag = true; return ; } if(step == 19) return ; dfs(x * 10, step + 1, n); dfs(x * 10 + 1, step + 1, n); } int main(void){ int n; while((scanf("%d", &n)) != EOF){ if(!n) break; flag = false; dfs(1, 0, n); } return 0; }