Java 蛮力:查找数字

Java 蛮力:查找数字,java,loops,if-statement,for-loop,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);

对于我的Intro CS类,我们必须创建一个程序来查找某个数字,在本例中是一个地址。地址在1000和9999之间,必须满足以下标准:

  • 所有四个数字都不同
  • 千位数字是十位数字的三倍
  • 这个数字是奇数
  • 数字之和是27
到目前为止,我已经能够生成数字的范围并缩小奇数,但其余的都很混乱。建议

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

for(int i=1000;i首先将每个值拆分为四个独立的数字,然后应用规则:

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);

                }
            }
        }
    }
}

for(int i=1000;i首先将每个值拆分为四个独立的数字,然后应用规则:

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);

                }
            }
        }
    }
}

for(int i=1000;i首先将每个值拆分为四个独立的数字,然后应用规则:

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);

                }
            }
        }
    }
}

for(int i=1000;i首先将每个值拆分为四个独立的数字,然后应用规则:

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);

                }
            }
        }
    }
}

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

编辑通过将标识计算为,可以消除其中一个循环

    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);
                }
            }
        }
    }

对于(int a=3;a<10;a+=3){/我将使用四个嵌套的
进行
循环。请记住,最右边的数字范围是
1-9
,最左边的数字范围是
3-9
(因为它是3*10s),而中间的数字范围是
0-9
(因为数字必须是奇数,每个数字必须是唯一的)

编辑通过将标识计算为,可以消除其中一个循环

    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);
                }
            }
        }
    }

对于(int a=3;a<10;a+=3){/我将使用四个嵌套的
进行
循环。请记住,最右边的数字范围是
1-9
,最左边的数字范围是
3-9
(因为它是3*10s),而中间的数字范围是
0-9
(因为数字必须是奇数,每个数字必须是唯一的)

编辑通过将标识计算为,可以消除其中一个循环

    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);
                }
            }
        }
    }

对于(int a=3;a<10;a+=3){/我将使用四个嵌套的
进行
循环。请记住,最右边的数字范围是
1-9
,最左边的数字范围是
3-9
(因为它是3*10s),而中间的数字范围是
0-9
(因为数字必须是奇数,每个数字必须是唯一的)

编辑通过将标识计算为,可以消除其中一个循环

    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);
                }
            }
        }
    }

对于(int a=3;a<10;a+=3){/轻度优化的解决方案:

/*
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);
}
/*
所有四个数字都不同
千位数字是十位数字的三倍
这个数字是奇数
数字之和是27
*/
//从4位数开始,只迭代奇数

对于(int i=1001;i轻度优化的解决方案:

/*
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);
}
/*
所有四个数字都不同
千位数字是十位数字的三倍
这个数字是奇数
数字之和是27
*/
//从4位数开始,只迭代奇数

对于(int i=1001;i轻度优化的解决方案:

/*
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);
}
/*
所有四个数字都不同
千位数字是十位数字的三倍
这个数字是奇数
数字之和是27
*/
//从4位数开始,只迭代奇数

对于(int i=1001;i轻度优化的解决方案:

/*
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);
}
/*
所有四个数字都不同
千位数字是十位数字的三倍
这个数字是奇数
数字之和是27
*/
//从4位数开始,只迭代奇数

对于(inti=1001;我猜是
System.out.println(9837)
不是一个可以接受的答案吗?好的,第一步是把数字分开,这样你就可以分别处理它们。你可以用
/
%
操作符来做这件事,或者把
int
转换成
字符串并查看其中的字符。
千位数字是三倍
意味着在3010-3919或6020-6929或9030-9939范围内-可以大大减少处理量参见
String.valueOf(i).toCharArray()
很好的答案伙计们。更多的“耶,我能解决问题,让我帮你做作业”正是需要的。如果你想玩代码高尔夫,我猜
System.out.println(9837)
不是一个可以接受的答案吗?好的,第一步是把数字分开,这样你就可以分别处理它们。你可以用
/
%
操作符来做这件事,或者把
int
转换成
字符串并查看其中的字符。
千位数字是三倍
意味着在3010-3919或6020-6929或9030-9939范围内-可以大大减少处理量参见
String.valueOf(i).toCharArray()
很好的答案伙计们。更多的“耶,我能解决问题,让我帮你做作业”正是需要的。如果你想玩代码高尔夫,我猜
System.out.println(9837)
不是一个可以接受的答案吗?好的,第一步是把数字分开,这样你就可以分别处理它们。你可以用
/
%
操作符来做这件事,或者把
int
转换成
字符串并查看其中的字符。
千位数字是三倍
意味着在3010-3919或6020-6929或9030-9939范围内-可以大大减少处理量参见
String.valueOf(i).tocharray()
很好的答案伙计们。更多“耶,我能解决问题,让我帮你做作业”正是需要的。如果你想玩代码高尔夫,