我如何处理逻辑问题?
How do I approach logic questions?
我在面试中遇到了这个问题。我要展示我在 PHP:
中的逻辑
John, Alex, Jay, Thomson and May live on different floors of an apartment house that contains only five floors. John does not live on the top floor. Alex does not live on the bottom floor. Jay does not live on either the top or the bottom floor. Thomson lives on a higher floor than does Alex. May does not live on a floor adjacent to Jay’s. Jay does not live on a floor adjacent to Alex’s. Where does everyone live?
我打算如何处理这类问题?如果有什么书籍或培训我可以得到?
我最初的想法是找出 "begin" 的位置,因为必须先完成其中的每一项。
这是我的做法。只需遍历所有可能的组合。
<?php
for ($alex = 1; $alex <= 5; $alex++) {
// note that it states alex does not live on the bottom floor,
// so you could start alex at 2 here, but then you would have
// to apply logic to the other counts too, and that will start to
// get complicated.
for ($john = 1; $john <= 5; $john++) {
for ($jay = 1; $jay <= 5; $jay++) {
for ($thomson = 1; $thomson <= 5; $thomson++) {
for ($may = 1; $may <= 5; $may++) {
// John, Alex, Jay, Thomson and May live on different floors of an apartment house that contains only five floors
if (count(array_unique(array($alex, $john, $jay, $thomson, $may))) !== 5) {
continue;
}
// John does not live on the top floor
if ($john == 5) {
continue;
}
// Alex does not live on the bottom floor
if ($alex == 1) {
continue;
}
// Jay does not live on either the top or the bottom floor
if ($jay == 1 || $jay == 5) {
continue;
}
// Thomson lives on a higher floor than does Alex
if ($thomson < $alex) {
continue;
}
// May does not live on a floor adjacent to Jay’s
if (abs($may - $jay) == 1) {
continue;
}
// Jay does not live on a floor adjacent to Alex’s
if (abs($jay - $alex) == 1) {
continue;
}
echo 'Alex: floor ' . $alex . '<br>';
echo 'John: floor ' . $john . '<br>';
echo 'Jay: floor ' . $jay . '<br>';
echo 'Thomson: floor ' . $thomson . '<br>';
echo 'May: floor ' . $may . '<br>';
}
}
}
}
}
解决方案/输出:
Alex: floor 2
John: floor 3
Jay: floor 4
Thomson: floor 5
May: floor 1
我在面试中遇到了这个问题。我要展示我在 PHP:
中的逻辑John, Alex, Jay, Thomson and May live on different floors of an apartment house that contains only five floors. John does not live on the top floor. Alex does not live on the bottom floor. Jay does not live on either the top or the bottom floor. Thomson lives on a higher floor than does Alex. May does not live on a floor adjacent to Jay’s. Jay does not live on a floor adjacent to Alex’s. Where does everyone live?
我打算如何处理这类问题?如果有什么书籍或培训我可以得到?
我最初的想法是找出 "begin" 的位置,因为必须先完成其中的每一项。
这是我的做法。只需遍历所有可能的组合。
<?php
for ($alex = 1; $alex <= 5; $alex++) {
// note that it states alex does not live on the bottom floor,
// so you could start alex at 2 here, but then you would have
// to apply logic to the other counts too, and that will start to
// get complicated.
for ($john = 1; $john <= 5; $john++) {
for ($jay = 1; $jay <= 5; $jay++) {
for ($thomson = 1; $thomson <= 5; $thomson++) {
for ($may = 1; $may <= 5; $may++) {
// John, Alex, Jay, Thomson and May live on different floors of an apartment house that contains only five floors
if (count(array_unique(array($alex, $john, $jay, $thomson, $may))) !== 5) {
continue;
}
// John does not live on the top floor
if ($john == 5) {
continue;
}
// Alex does not live on the bottom floor
if ($alex == 1) {
continue;
}
// Jay does not live on either the top or the bottom floor
if ($jay == 1 || $jay == 5) {
continue;
}
// Thomson lives on a higher floor than does Alex
if ($thomson < $alex) {
continue;
}
// May does not live on a floor adjacent to Jay’s
if (abs($may - $jay) == 1) {
continue;
}
// Jay does not live on a floor adjacent to Alex’s
if (abs($jay - $alex) == 1) {
continue;
}
echo 'Alex: floor ' . $alex . '<br>';
echo 'John: floor ' . $john . '<br>';
echo 'Jay: floor ' . $jay . '<br>';
echo 'Thomson: floor ' . $thomson . '<br>';
echo 'May: floor ' . $may . '<br>';
}
}
}
}
}
解决方案/输出:
Alex: floor 2
John: floor 3
Jay: floor 4
Thomson: floor 5
May: floor 1