蛮力:找到数字

时间:2014-11-07 05:08:15

标签: java loops if-statement for-loop

对于我的Intro CS类,我们必须创建一个程序,找到一个特定的数字,在本例中是一个地址。地址在1000和9999之间,必须符合以下标准:

  • 所有四位数字都不同
  • 千位的数字是十位数的三倍
  • 数字是奇数
  • 数字之和为27

到目前为止,我已经能够生成数字范围并缩小数字范围,但其余部分非常令人困惑。建议?

for (int i = 1000; i <= 9999; i++)
    {
        if (i % 2 == 1)
            System.out.print(i);
        else
            System.out.println();
    }   

3 个答案:

答案 0 :(得分:2)

我会使用四个嵌套的for循环。请记住,最右边的数字范围是1 - 9,最左边是3 - 9(因为它是3 * 10s),但中间对的数字0 - 9(因为数字必须是是奇数,每个数字必须是唯一的),

for (int a = 3; a < 10; a++) {
    for (int b = 0; b < 10; b++) {
        if (a == b) // unique check
            continue;
        for (int c = 1; c < 10; c++) {
            if (a == c || b == c || a != 3 * c) // unique check && # 1000s=3*10s
                continue;
            for (int d = 1; d < 10; d += 2) { // # must be odd.
                if (a == d || b == d || c == d) // unique check
                    continue;
                if (a + b + c + d == 27) { // # must sum to 27.
                    System.out.printf("%d%d%d%d%n", a, b, c, d);
                }
            }
        }
    }
}

输出

9837

编辑您可以通过将身份计算为

来消除其中一个循环
    for (int a = 3; a < 10; a += 3) { // <-- a is a multiple of 3.
        for (int b = 0; b < 10; b++) {
            if (a == b) // unique check
                continue;
            int c = a / 3; // 1000s = 3*10s
            if (b == c)
                continue;
            for (int d = 1; d < 10; d += 2) { // # must be odd.
                if (a == d || b == d || c == d) // unique check
                    continue;
                if (a + b + c + d == 27) { // # must sum to 27.
                    System.out.printf("%d%d%d%d%n", a, b, c, d);
                }
            }
        }
    }

答案 1 :(得分:1)

首先将每个值拆分为四个单独的数字,然后应用规则:

for (int i = 1000; i <= 9999; i++)
{
    int ones = i % 10;
    int tens = (i / 10) % 10;
    int hundreds = (i / 100) % 10;
    int thousands = i / 1000;

    // rule 1
    if(ones != tens &&
            ones != hundreds &&
            ones != thousands &&
            tens != hundreds &&
            tens != thousands &&
            hundreds != thousands) {

        // rule 2
        if(thousands == 3 * tens) {

            // rule 3
            if(ones % 2 == 1) {

                // rule 4
                if(ones + tens + hundreds + thousands == 27) {

                    System.out.println(i);

                }
            }
        }
    }
}

此解决方案可以进行优化(例如首先测试奇数),但只有在解决方案有效后才能进行优化(在测量之前不应进行优化)。

答案 2 :(得分:0)

轻度优化的解决方案:

/*
All four digits are different
The digit in the thousands place is three times the digit in the tens place
The number is odd
The sum of the digits is 27
*/

// Start at 4 digit numbers, only iterate odd numbers
for (int i = 1001; i <= 9999; i += 2) {

    // break the four digits into seperate variables
    int num4 = i % 10;
    int num3 = i / 10 % 10;
    int num2 = i / 100 % 10;
    int num1 = i / 1000 % 10;

    // check that the "first" number is 3x the "third"
    if (num1 != (num3 * 3))
        continue;

    // make sure all numbers are unique
    if (num1 != num2 && num1 != num3 && num1 != num4 && num2 != num3 && num2 != num4 && num3 != num4)
        // and they sum to 27
        if ((num1 + num2 + num3 + num4) == 27)
            System.out.println(i);
}

答案当然是9837