所以我尝试了这个程序,它应该找出这些门的位置。它们开始关闭,然后你切换每扇门。下次你每秒切换一次,然后每隔三,四,等等。我得到了正确的编码,但它似乎效率很低。如果我用更大的数字尝试这个,那么程序将花费太长时间。有没有办法更有效地做到这一点?这是我的编码:
public class doors
{
public static void main(String [] args)
{
int x, y = 1;
boolean [] doors = new boolean[100];
for (int a = 0; a < doors.length; a++)
doors[a] = false; //false means closed
do
{
for (x = 0; x < doors.length - 1; x++)
{
if ((x+1) % y == 0)
{
if (doors[x] == true)
doors[x] = false;
else if (doors[x] == false)
doors[x] = true;
}
}
y++;
}while (y <= doors.length);
for (boolean b : doors)
System.out.println(b);
}
}
// Thanks!!!
答案 0 :(得分:2)
让我们一步一步地简化代码。
1)分配数组时,所有元素都会初始化为其默认值,即0
,false
或null
。在您的情况下,这意味着false
,因此无需循环并执行此操作。
// before
boolean [] doors = new boolean[100];
for (int a = 0; a < doors.length; a++)
doors[a] = false; //false means closed
// after
boolean[] doors = new boolean[100];
2a)由于数组不是零长度数组,do-while
循环也可能是正常的while
循环(它至少会执行一次)。
2b)作为一个普通的while
循环,在循环之前有一个初始化程序,在循环结束时有一个增量,它与for
循环相同。
2c)由于循环后未使用y
,因此可以在循环中声明它。
// before
int x, y = 1;
do
{
//code
y++;
}while (y <= doors.length);
// after
int x;
for (int y = 1; y <= doors.length; y++)
{
//code
}
3a)doors[x] == true
与doors[x]
相同。 doors[x] == false
与! doors[x]
相同。
3b)if (x) {} else if (! x) {}
与if (x) {} else {}
相同。 else
之后的测试是多余的。
3c)if (x) x = false; else x = true;
与x = ! x;
相同。
3d)您可以通过^= true
(XOR)跳过双重评估。
// before
if (doors[x] == true)
doors[x] = false;
else if (doors[x] == false)
doors[x] = true;
// after
doors[x] = ! doors[x];
// skip double evaluation of index lookup
doors[x] ^= true;
4a)(x+1) % y == 0
仅适用于x
,y-1
,2*y-1
的{{1}}值,依此类推,因此make loop start at { {1}}并按3*y-1
递增,无需y-1
语句。
4c)由于循环后未使用y
,因此可以在循环中声明它。
if
到目前为止的结果(在删除不必要的大括号后)
x
5a)将// before
int x;
for (x = 0; x < doors.length - 1; x++)
{
if ((x+1) % y == 0)
{
//code
}
}
// after
for (int x = y - 1; x < doors.length - 1; x += y)
{
//code
}
限制为boolean[] doors = new boolean[100];
for (int y = 1; y <= doors.length; y++)
for (int x = y - 1; x < doors.length - 1; x += y)
doors[x] ^= true;
for (boolean b : doors)
System.out.println(b);
意味着永远不会使用/更新数组的最后一个值。放下x
。
< doors.length - 1
6a)每行打印一个布尔- 1
/ // before
for (int x = y - 1; x < doors.length - 1; x += y)
// after
for (int x = y - 1; x < doors.length; x += y)
值的数组并不是人类可读的。打印在一行上作为二进制数。
true
7a)在外环内移动打印,你会看到一个有趣的图案。
false
// before
for (boolean b : doors)
System.out.println(b);
// after
for (boolean b : doors)
System.out.print(b ? '1' : '0');
System.out.println();
现在,当您查看结果模式(最后一行)时,您会看到您的代码可以缩减为(非常快):
boolean[] doors = new boolean[100];
for (int y = 1; y <= doors.length; y++) {
for (int x = y - 1; x < doors.length; x += y)
doors[x] ^= true;
for (boolean b : doors)
System.out.print(b ? '1' : '0');
System.out.println();
}
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
1010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010
1000111000111000111000111000111000111000111000111000111000111000111000111000111000111000111000111000
1001111100101001111100101001111100101001111100101001111100101001111100101001111100101001111100101001
1001011101101011111000100001101100001000111110101101110100111001011101101011111000100001101100001000
1001001101111011101000110001111100011000101110111101100100101001001101111011101000110001111100011000
1001000101111111101010110000111100111000111110110101100000101011001100111011001000100001110100011100
1001000001111110101010100000111000111001111110100101100100101010001100101011001100100000110100001100
1001000011111110111010100010111000101001111100100101110100101000001100111011001110100000100100001110
1001000010111110111110100010101000101000111100100001110100111000001101111011001010100000110100001111
1001000010011110111111100010101010101000111000100001111100111000011101111011101010100001110100001101
1001000010001110111111110010101010111000111000110001111100101000011101101011101010110001110100011101
1001000010000110111111110110101010111010111000110000111100101000111101101011111010110001111100011101
1001000010000010111111110111101010111010101000110000111000101000111100101011111010100001111100011001
1001000010000000111111110111111010111010101010110000111000111000111100101001111010100001101100011001
1001000010000001111111110111111110111010101010100000111000111001111100101001111110100001101100001001
1001000010000001011111110111111111111010101010100010111000111001111000101001111110101001101100001001
1001000010000001001111110111111111101010101010100010101000111001111000111001111110101001111100001001
1001000010000001000111110111111111101110101010100010101010111001111000111000111110101001111100101001
1001000010000001000011110111111111101111101010100010101010101001111000111000111010101001111100101000
1001000010000001000001110111111111101111111010100010101010101011111000111000111010111001111100101000
1001000010000001000000110111111111101111111110100010101010101011101000111000111010111000111100101000
1001000010000001000000010111111111101111111111100010101010101011101010111000111010111000111000101000
1001000010000001000000000111111111101111111111110010101010101011101010101000111010111000111000111000
1001000010000001000000001111111111101111111111110110101010101011101010101010111010111000111000111001
1001000010000001000000001011111111101111111111110111101010101011101010101010101010111000111000111001
1001000010000001000000001001111111101111111111110111111010101011101010101010101000111000111000111001
1001000010000001000000001000111111101111111111110111111110101011101010101010101000101000111000111001
1001000010000001000000001000011111101111111111110111111111101011101010101010101000101010111000111001
1001000010000001000000001000001111101111111111110111111111111011101010101010101000101010101000111001
1001000010000001000000001000000111101111111111110111111111111111101010101010101000101010101010111001
1001000010000001000000001000000011101111111111110111111111111110101010101010101000101010101010101001
1001000010000001000000001000000001101111111111110111111111111110111010101010101000101010101010101011
1001000010000001000000001000000000101111111111110111111111111110111110101010101000101010101010101011
1001000010000001000000001000000000001111111111110111111111111110111111101010101000101010101010101011
1001000010000001000000001000000000011111111111110111111111111110111111111010101000101010101010101011
1001000010000001000000001000000000010111111111110111111111111110111111111110101000101010101010101011
1001000010000001000000001000000000010011111111110111111111111110111111111111101000101010101010101011
1001000010000001000000001000000000010001111111110111111111111110111111111111111000101010101010101011
1001000010000001000000001000000000010000111111110111111111111110111111111111111100101010101010101011
1001000010000001000000001000000000010000011111110111111111111110111111111111111101101010101010101011
1001000010000001000000001000000000010000001111110111111111111110111111111111111101111010101010101011
1001000010000001000000001000000000010000000111110111111111111110111111111111111101111110101010101011
1001000010000001000000001000000000010000000011110111111111111110111111111111111101111111101010101011
1001000010000001000000001000000000010000000001110111111111111110111111111111111101111111111010101011
1001000010000001000000001000000000010000000000110111111111111110111111111111111101111111111110101011
1001000010000001000000001000000000010000000000010111111111111110111111111111111101111111111111101011
1001000010000001000000001000000000010000000000000111111111111110111111111111111101111111111111111011
1001000010000001000000001000000000010000000000001111111111111110111111111111111101111111111111111111
1001000010000001000000001000000000010000000000001011111111111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001001111111111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000111111111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000011111111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000001111111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000111111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000011111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000001111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000111110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000011110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000001110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000110111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000010111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000000111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001111111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001011111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001001111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000111111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000011111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000001111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000111111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000011111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000001111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000111111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000011111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000001111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000111101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000011101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000001101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000101111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000001111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000011111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010111111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010011111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010001111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000111111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000011111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000001111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000111111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000011111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000001111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000111111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000011111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000001111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000000111110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000000011110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000000001110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000000000110
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000000000010
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000000000000
1001000010000001000000001000000000010000000000001000000000000001000000000000000010000000000000000001
答案 1 :(得分:1)
首先,doors
数组中的默认值为false(但如果需要,可以使用Arrays.fill
填充它)。接下来,您只需要将循环增加y
。然后,您可以使用doors[x] = !doors[x];
切换。像,
int y = 1;
boolean[] doors = new boolean[100];
// Arrays.fill(doors, false);
do {
for (int x = y - 1; x < doors.length; x += y) {
doors[x] = !doors[x];
}
y++;
} while (y <= doors.length);
System.out.println(Arrays.toString(doors));
或使用嵌套的for
循环,例如
boolean[] doors = new boolean[100];
// Arrays.fill(doors, false);
for (int y = 1; y < doors.length; y++) {
for (int x = y - 1; x <= doors.length; x += y) {
doors[x] = !doors[x];
}
}
System.out.println(Arrays.toString(doors));
答案 2 :(得分:0)
正如上面提到的那样,你可以做很多事情来简化代码。通过这样做,您将开始更好地查看正在使用的算法,从而了解它们的改进位置。
使用这样的代码,简化像if语句这样的事情通常更多的是澄清代码而不是提高性能。我之所以这么说的原因是编译器这些天非常好,因为优化代码虽然可能会丢失一些指令,但性能差异可能很小。
如果有循环,你更有可能获得明显改善的地方。减少循环中的迭代次数通常会对总体时间产生显着影响。为此,我可以看到两个可以改进的地方。
首先,可能不需要初始设置循环。我没有检查过,但默认情况下门阵列可能是假的,因此可能会丢弃循环到初始化。
其次,内部循环递增1并测试该门是否与y匹配。你可以通过简单地用y增加内部循环来短路很多。这样你就会一直在想要翻转的门上。这意味着你可以放弃正确门的测试,然后翻转它。随着y的增加,x循环的迭代次数将减少。这意味着每次y都会加速。
最后,当我看到这样的代码时,我总是推荐一件事。始终在if语句中加上括号。当不使用括号时,很容易引入细微的错误。
答案 3 :(得分:-6)
而不是使用for语句,请使用foreach并切换
示例
for(boolean i:doors){
//do something
}