Find The Multiple

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

思路: dfs和bfs都可以,就是考虑怎么除,如果直接除的话递归时后面那个数是前面的乘10或者乘10+1就可以了,,但是这么就比较不好求,因为数字太大,不能优化,取模哪有那么快,那就可以用同余做,递归的时候直接递归 (num×10)%n或者再加上1取模 ,但是这样就有一个问题这么递归的话,输出那个数字句没法输出了,因为递归中就没有写那个数字,这个 很好解决呀,,再传一个参数数字这个数字就可以了呀,或者用数组去存每一位的数字
另一个问题考虑怎么存,,开始我用int表示,答案是负数,,之后换成long 还是负数,如果用 Biginteger就不好写取模,,在c中有longlong,,,这个的确比较头疼啊,所以我这里也只能用数组了。

代码方面 刚开始我没有每次dfs之前把solve设成0,导致样例只能输出一组,,粗心的一批,,

还有一个梗,刚开始看到答案不一样一直以为自己错了,之后才发现 dfs的话会长一点,因为他会一条1递归到最深出,然后bfs的话会短一点,最优解嘛,你懂得。
错误代码:

    import java.util.Scanner;
    //这道题没有必要去一位一位的搜索判断是否为1或者0 啊,直接按数字计算就好了
    //我刚开始的想法就是第一位是0,第二味是1,是不一样的,入口怎么写?
import java.util.Scanner;

class Kruskal{

    static int n;
    static  int m;
    static int solve=0;

    public static void main(String[] args) {
        Scanner sc  = new Scanner(System.in);
        while (true){
            n= sc.nextInt();
            if(n==0) break;
            solve  = 0;
            dfs(1,1,1);
        }
    }
    static void dfs(int num,int k ,long ori){
        if(solve  == 1){//为了防止输出多个
            return;
        }
        if(num==0){
            System.out.println(ori);
            solve = 1;
            return;
        }
        if(k>=100){
            return;
        }
        dfs((num*10)%n,k+1,ori*10);
        dfs((num*10+1)%n,k+1,ori*10+1);
    }
}

bfs写法:都不用去想要递归多少次了

    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Scanner;
    //这道题没有必要去一位一位的搜索判断是否为1或者0 啊,直接按数字计算就好了
    //我刚开始的想法就是第一位是0,第二味是1,是不一样的,入口怎么写?
    class Main{

        static int n;
        static  int m;

        static Queue<Integer> queue = new LinkedList<>();
        public static void main(String[] args) {
            Scanner sc  = new Scanner(System.in);
            while (true){
                n= sc.nextInt();
                if(n==0) break;

                bfs();
            }
        }

        static void bfs(){
            queue.add(1);
            while (!queue.isEmpty()){
                Integer poll = queue.poll();
                for(int i  =0;i<=1;i++){
                    int x = poll*10+i;
                    if(x%n==0){
                        System.out.println(x);
                        return;
                    }
                    queue.add(x);
                }
            }
        }
    }

数组存法:有一点点问题

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
//这道题没有必要去一位一位的搜索判断是否为1或者0 啊,直接按数字计算就好了
//我刚开始的想法就是第一位是0,第二味是1,是不一样的,入口怎么写?
class test2{

    static int n;
    static  int m;
    static int s[] = new int[100];
    static int flag = 0;
    static int end = 0;
    static Queue<Integer> queue = new LinkedList<>();
    public static void main(String[] args) {
        Scanner sc  = new Scanner(System.in);
        while (true){
            n= sc.nextInt();
            if(n==0) break;
            s[0] = 1;
            flag = 0;
            dfs(1,1,n);
            for(int i = 0;i<end;i++){
                System.out.print(s[i]+" ");
            }
        }
    }

    static void dfs(int mod,int d,int n){
        if(d>100) return;
        if(mod==0){
            flag = 1;
            end = d;
            return;
        }
        if(flag ==1) return;
        else {
            s[d]= 0;
            dfs((mod*10)%n,d+1,n);

            s[d] = 1;
            dfs((mod*10+1)%n,d+1,n);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值