涉及计算出10位数字拼图的算法

时间:2016-05-03 04:28:22

标签: java algorithm

要确定某个数字是否可以被7整除,请取出该数字的最后一位数字,将其加倍并从剩余数字中减去加倍数字。如果结果可被7整除(例如14,7,0,-7等),则该数字可被7整除。这可能需要重复几次。 示例:3101是否可被7整除?

 310   - take off the last digit of the number which was 1
  -2   - double the removed digit and subtract it
 308   - repeat the process by taking off the 8
-16    - and doubling it to get 16 which is subtracted
 14    - the result is 14 which is a multiple of 7 

以下是我为获取号码所做的代码:

    for(int O =0; O <= 9 ; O++) {
            String a = String.valueOf(number[0]);
            String b = String.valueOf(number[1]);
            String c = String.valueOf(number[2]);
            String d = String.valueOf(number[3]);
            String e = String.valueOf(number[4]);
            String f = String.valueOf(number[5]);

            String h = a+b+c+d+e+f;
            int abcdef = Integer.valueOf(h);
            if ( (abcdef -(2*O) % 7) ==0 )
                number [6] = O;

    }

然而,它并没有给我一些这样的。我能够得到一个数字直到6,直到每个数字,该数字可以被相应的索引整除(如果我从1开始,而不是0为索引)。这意味着索引1可以被1整除,索引2可以被2整除,索引3可以被3整除,......索引7可以被7整除。我想形成一些这样的类型。注意我可以通过以下方式不使用算法来完成它:

     for(int O =0; O <= 9 ; O++) {
        String a = String.valueOf(number[0]);
        String b = String.valueOf(number[1]);
        String c = String.valueOf(number[2]);
        String d = String.valueOf(number[3]);
        String e = String.valueOf(number[4]);
        String f = String.valueOf(number[5]);
        String g = String.valueOf(O);
        String h = a+b+c+d+e+f+g;
        int abcdefg = Integer.valueOf(h);
        if ( (abcdefg % 7) ==0 )
            number [6] = O;

}

但是,我真的想使用我在开头描述的算法来做到这一点。

1 个答案:

答案 0 :(得分:0)

调用代码示例

int [] num = new int [7];
for (int i = 1000000; i < 9999999; i++)
{
    // put i into the array and check it
    if (checkDigitsDivisible(i, num))
    {
        System.out.println(i);
    }
}

检查一个数字的第一个数字是否可被1整除,前两个数字可被2整除,前三个数字可被3整除,等等。

public static boolean checkDigitsDivisible (int num, int [] arr)
{
    int one = num / 1000000;
    int two = num / 100000;
    int three = num / 10000;
    int four = num / 1000;
    int five = num / 100;
    int six = num / 10;
    int seven = num;

    arr[0] = one % 10;
    arr[1] = two % 10;
    arr[2] = three % 10;
    arr[3] = four % 10;
    arr[4] = five % 10;
    arr[5] = six % 10;
    arr[6] = seven % 10;

    return (one % 1 == 0) && 
           (two % 2 == 0) && 
           (three % 3 == 0) && 
           (four % 4 == 0) && 
           (five % 5 == 0) && 
           (six % 6 == 0) &&
           (isDivisibleBy7(seven));
}

检查数字是否可被7整除的递归解决方案(问题中描述的算法):

public static boolean isDivisibleBy7 (int n)
{       
    n = Math.abs(n);

    if (n == 0 || n == 7 || n == 14)
    {
        return true;
    }
    else
    {
        int lastDigit = n % 10;
        int lastDigitOff = n / 10;

        int remainder = lastDigitOff - (lastDigit * 2);

        remainder = Math.abs(remainder);

        if (remainder > n)
        {
            return false;
        }

        return isDivisibleBy7(remainder);
    }
}