java回溯生成数字

时间:2012-10-18 16:07:13

标签: java backtracking

我写了一个小程序来生成有两个条件的数字:

  1. 的所有数字均来自1 tot 9,所以数字如123456789
  2. 该数字必须可以被最后一位数整除     例如442因为4%2 == 0和4%4 == 0
  3. 这是我的回溯算法:

    static void backTrack(int value)
        {
            //Check if the number has all 9 digits, that it is dividable
            if(isNine(value) && isDiv(value))
            {
                //System.out.println(value);
                System.out.println("Found solution.");
                System.out.println(aantal);
                aantal++;
            }
            else
            {
    
                if(howMany(value) >= 9)
                    return;
    
                for(int i = 1; i < 10; i++)
                {
                    value = value * 10 + i;
                    if(value % i == 0  && howMany(value) <= 9)
                    {
                        //System.out.println(value);
                        backTrack(value);
                    }
                    value = value / 10;
                }
            }
        }
    
        //Gives length of integer for example 124 must give 3, 13 gives 2
        static int howMany(int value)
        {
            int test = value % 10;
            value = value / 10;
            int teller = 0;
    
            while(test != 0)
            {
                teller++;
                test = value % 10;
                value = value / 10;
            }
            return teller;
        }
    
        //Checks if the number is dividable by the last digit of the number and keeps recursive doing this for the whole number so 442 = YES 235 = NO
        static boolean isDiv(int value)
        {
            int test = value % 10;
            value = value / 10;
    
            while(test != 0)
            {
                if(value % test == 0)
                {
                    test = value % 10;
                    value = value / 10;
                }
                else
                    return false;
            }
            return true;
        }
    
        //Checks if the number has all digits from 1 to 9
        static boolean isNine(int value)
        {
            boolean values[] = new boolean[10];
            int test = value % 10;
            int counter = 0;
    
            for(int i = 1; i < values.length; i++)
                values[i] = false;
    
            while( test != 0)
            {
                if(values[test])
                    return false;
                else
                {
                    values[test] = true;
                    value = value /10;
                    test = value % 10;
                }
            }
    
            for(int i = 1; i < values.length; i++)
            {
                if(values[i])
                    counter++;
            }
    
            if(counter == 9)
                return true;
            else
                return false;
        }
    

    它从来没有找到解决方案,我测试了所有子功能,而且这些功能都很好。

    我的回溯计划有问题吗? System.out.println(aantal)只是一个可以计算我找到了多少解决方案的var。

    我从backtrack(0);

    开始

1 个答案:

答案 0 :(得分:0)

static void backTrack(int value)
    {
        //Check if the number has all 9 digits, that it is dividable
        if(isNine(value)) // CHANGED this if test. 
        {
            System.out.println("Found solution.");
            System.out.println(value);
            aantal++;
        }
        else
        {

            if(howMany(value) >= 9)
                return;

            for(int i = 1; i < 10; i++)
            {
                value = value * 10 + i;
                if(value % i == 0  && howMany(value) <= 9)
                {
                    //System.out.println(value);
                    backTrack(value);
                }
                value = value / 10;
            }
        }
    }

    //Gives length of integer for example 124 must give 3, 13 gives 2
    static int howMany(int value)
    {
        int test = value % 10;
        value = value / 10;
        int teller = 0;

        while(test != 0)
        {
            teller++;
            test = value % 10;
            value = value / 10;
        }
        return teller;
    }


    //Checks if the number has all digits from 1 to 9
    static boolean isNine(int value)
    {
        boolean values[] = new boolean[10];
        int test = value % 10;
        int counter = 0;

        for(int i = 1; i < values.length; i++)
            values[i] = false;

        while( test != 0)
        {
            if(values[test])
                return false;
            else
            {
                values[test] = true;
                value = value /10;
                test = value % 10;
            }
        }

        for(int i = 1; i < values.length; i++)
        {
            if(values[i])
                counter++;
        }

        if(counter == 9)
            return true;
        else
            return false;
    }
}