1

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

  1. 有从 1 到 9 的所有数字,所以像 123456789 这样的数字
  2. 该数字必须能被最后一位整除,例如 442,因为 4 % 2 == 0 和 4 % 4 == 0

这是我的回溯算法:

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)只是一个变量来计算我找到了多少解决方案。

我从backtrack(0);

4

1 回答 1

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;
    }
}
于 2012-10-18T16:21:52.870 回答