Find The Multiple(简单搜索)

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

题目描述:
时间来到20210年,科学家终于发明出了与二进制星人沟通的方式。二进制星人只能理解完全由1和0组成的数字,聪明的科学家发现,如果想表示数字n,只要用数字1和数字0写出一个为n的倍数的数字,把它拿给二进制星人看,他们就能理解了。如想表示6这个数字,只要拿着“100100100100100100”(6的16683350016683350倍)给二进制星人看,他就会明白这是6的意思了。给出n,请你模拟这一过程。
提示:本题采用Special Judge,你无需输出所有符合条件的m,你只需要输出任一符合条件的m即可。
Input
输入包含多组数据,每组数据仅一行,只包含一个正整数n,n==0时输入结束 (1 <= n <= 200).
Output
对于输入的每组n,都输出任一符合条件的m,m为只由1和0构成的十进制数,且m是n的倍数。即使有多个符合条件的m,你也只需要输出一个即可。
Sample Input
2
6
19
0
Sample Output
10
100100100100100100
111111111111111111

本题用简单的查找就可以做出来,因为我们要查找的数必须是只能有1或0因此我们就从1开始,进行深搜,每次要不就是乘于10要不就加1,直到找到可以被能整除的数,直接上代码:

#include <iostream>
#include <cstdio>
#define ll unsigned long long

using namespace std;

long long n,sum,ans;
bool f;

void DFS(ll sum) {
    
    
	if(sum > 1111111111111111111LL ||sum < 0||f)//判断如果找的数据已经大于1e18那就直接返回
		return ;
	if(sum % n == 0) {
    
    //如果出现可以被n整除的数就直接返回
		ans = sum;
		f = 1;
		return ;
	}
	DFS(sum*10);//相当于在末尾加0

	DFS(sum*10+1);//相当于在末尾加1
}
int main () {
    
    
	while(cin >> n&&n) {
    
    
		f = 0;
		DFS(1);//从1开始进行深搜
		cout << ans << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_48627750/article/details/119494336
今日推荐