【问题标题】:How do I approach logic questions? [closed]我如何处理逻辑问题? [关闭]
【发布时间】:2015-03-04 02:06:51
【问题描述】:

我被问到这个问题是为了面试。我将展示我在 PHP 中的逻辑:

约翰、亚历克斯、杰伊、汤姆森和梅住在一栋只有五层楼的公寓的不同楼层。约翰不住在顶层。亚历克斯不住在底层。杰既不住在顶层也不住在底层。汤姆森住在比亚历克斯更高的楼层。 May 不住在 Jay's 附近的楼层。杰伊不住在亚历克斯家附近的楼层。大家都住在哪里?

我打算如何处理这些问题?如果我能得到某种书籍或培训?

我最初的想法是弄清楚从哪里“开始”,因为这些类型中的每一个都必须在另一个之前完成。

【问题讨论】:

  • 我不会得到这份工作,可能是件好事
  • 哈哈哈.. 我希望我能投票赞成
  • 为 5 个人设置变量名称,然后设置对应于每个语句的方程和不等式系统。布置一个表格(数组)来表示楼层/人员组合。使用任何方程来排除某些选项,然后将不等式递归地应用于其余选项。测试解决方案很容易,只需再次运行所有方程和不等式即可。
  • 这个问题很小,你可以尝试所有有效的组合并测试。
  • 但是,在您添加一些代码和关于该代码的特定问题之前,您可能会因为不够具体或与 SO 不够相关而遭到密切投票。

标签: php logic


【解决方案1】:

我会这样做。只需遍历所有可能的组合。

<?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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多