组合锁密码
Combination lock code
我正在尝试解决旧的密码锁程序。问题在下面。我已经写了一些代码,但到目前为止,它只打印了一堆可能的组合,这些组合总是包含第一个数字为 36 的数字。老实说,我非常困惑,不知道从哪里得到它需要的地方。
"Imagine you need to open a standard combination dial lock but don't know the combination and don't have a pair of bolt cutters. Write a Java program in BlueJ with a method that prints all possible combinations so you can print them on a piece of paper and check off each one as you try it. Assume the numbers on each dial range from zero to thirty-six and three numbers in sequence are needed to open the lock. Suppose the lock isn't a very good one and any number that's no more than two away from the correct number in each digit will also work. In other words if the combination is 17-6-32, then 18-5-31, 19-4-32, 15-8-33 and many other combinations will also open the lock. Write another method that prints out a minimal list of combinations you would need to try to guarantee opening the lock. "
/**
* A method that prints all possible combinations of the lock.
*/
public void combination(int combo)
{
int a;
int b;
int c;
a = 0;
while (a <= 36)
{b = 0;
while (b <= 36)
{c = 0;
while (c <= 36)
{
System.out.println(a + " " + b + " " + c);
c = c + 1;
}
b = b + 1;
}
a = a + 1;
}
}
}
您的程序运行良好。我的猜测是 BlueJ 限制了 consol 的大小。转到 Options
并打开 Unlimited Buffering
。这应该允许您查看所有可能的组合。如果你调试它并逐行通过它,你会看到所有组合都在输出,但由于显示行数的限制,它看起来好像第一个数字总是 36,因为你只得到最后这么多行。
旁注:您永远不会使用传递给 combinations() 的 int 组合参数,所以如果我是您,我会删除它。
我正在尝试解决旧的密码锁程序。问题在下面。我已经写了一些代码,但到目前为止,它只打印了一堆可能的组合,这些组合总是包含第一个数字为 36 的数字。老实说,我非常困惑,不知道从哪里得到它需要的地方。
"Imagine you need to open a standard combination dial lock but don't know the combination and don't have a pair of bolt cutters. Write a Java program in BlueJ with a method that prints all possible combinations so you can print them on a piece of paper and check off each one as you try it. Assume the numbers on each dial range from zero to thirty-six and three numbers in sequence are needed to open the lock. Suppose the lock isn't a very good one and any number that's no more than two away from the correct number in each digit will also work. In other words if the combination is 17-6-32, then 18-5-31, 19-4-32, 15-8-33 and many other combinations will also open the lock. Write another method that prints out a minimal list of combinations you would need to try to guarantee opening the lock. "
/**
* A method that prints all possible combinations of the lock.
*/
public void combination(int combo)
{
int a;
int b;
int c;
a = 0;
while (a <= 36)
{b = 0;
while (b <= 36)
{c = 0;
while (c <= 36)
{
System.out.println(a + " " + b + " " + c);
c = c + 1;
}
b = b + 1;
}
a = a + 1;
}
}
}
您的程序运行良好。我的猜测是 BlueJ 限制了 consol 的大小。转到 Options
并打开 Unlimited Buffering
。这应该允许您查看所有可能的组合。如果你调试它并逐行通过它,你会看到所有组合都在输出,但由于显示行数的限制,它看起来好像第一个数字总是 36,因为你只得到最后这么多行。
旁注:您永远不会使用传递给 combinations() 的 int 组合参数,所以如果我是您,我会删除它。