POJ 1426 Find The Multiple

女爷i 2021-09-30 23:46 309阅读 0赞

Find The Multiple
















Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 46927   Accepted: 19608   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

  1. 2
  2. 6
  3. 19
  4. 0

Sample Output

  1. 10
  2. 100100100100100100
  3. 111111111111111111

Source

Dhaka 2002

思路: 使用dfs枚举所有可能的结果(01串),当该结果可以整除n时,就输出。由于long long最多可以表示19位数字,所以当该数的位数大于19时,return。

  1. 1 #include <iostream>
  2. 2
  3. 3 using namespace std;
  4. 4
  5. 5 int n, flag;
  6. 6
  7. 7 void dfs(int k, long long cur) // k:位数 cur:当前数字
  8. 8 {
  9. 9 if (k == 19 || flag) // long long最多只能表示19位数
  10. 10 return;
  11. 11
  12. 12 if (cur % n == 0) {
  13. 13 cout << cur << endl;
  14. 14 flag = 1;
  15. 15 return;
  16. 16 }
  17. 17 for (int i = 0; i <= 1; ++i)
  18. 18 {
  19. 19 dfs(k + 1, cur * 10 + i);
  20. 20 }
  21. 21 }
  22. 22
  23. 23
  24. 24 int main()
  25. 25 {
  26. 26 while (cin >> n, n != 0) {
  27. 27 flag = 0;
  28. 28 dfs(0, 1);
  29. 29 }
  30. 30 return 0;
  31. 31 }

转载于:https://www.cnblogs.com/FengZeng666/p/10511604.html

发表评论

表情:
评论列表 (有 0 条评论,309人围观)

还没有评论,来说两句吧...

相关阅读