Find the number【 DFS】

Problem:
Given a number nn, let you find a decimal number m consisting only of 0,1, and require this positive integer m to be divisible by n.

Input format:
Input an integer n (1≤n<200).
Output format:
For each value of the input integer n, output the corresponding value of m, and ensure that there is a number with a length of less than 19 digits. If there are multiple solutions for a given value n, any one of them is acceptable.
The answer to this question is not unique, and all the answers that meet the requirements are correct.
Sample input
2
Sample output
10

#include<bits/stdc++.h>
using namespace std;
int flag = 0;
void dfs(int n, long long  x){
  if(flag) return ;
  if(x > 1e18) return ;
  if(x % n == 0){
    printf("%lld\n",x);
    flag = 1;
    return ;
  }

    dfs(n, x * 10);
  	dfs(n,x * 10 + 1);
  
}

int main()
{
  
  int n;
  scanf("%d",&n);
  dfs(n,1);
  return 0;
}

 

Guess you like

Origin blog.csdn.net/Mercury_Lc/article/details/105741054
Recommended