Interview Questions-PHP algorithmic logic: How to calculate age?

Source: Internet
Author: User
Tags php write
Topic:

经理有三个女儿,年龄相加为13。三个女儿的年龄相乘为经理的年龄,经理的一个手下知道 经理的年龄,但是不知道其三个女儿的年龄。经理告诉手下有一个女儿头发是黑色的,手下立即知道了三个女儿的年龄。请问三个女儿的年龄分别是多少?为什么?

Calculation:


function getAge($sum){ $ageLimit = 121; // 最大年龄121岁 $ageFrist = 18; //假设最小生育年龄 18岁 $posible = []; for ($c1 = 1; $c1 <= $sum; $c1++) { for ($c2 = 1; $c2 <= $sum; $c2++) { for ($c3 = 1; $c3 <= $sum; $c3++) { if ($c1 + $c2 + $c3 == $sum && $c1 * $c2 * $c3 < $ageLimit && $c1 * $c2 * $c3 - max($c1, $c2, $c3) >= $ageFrist) { $arr = [$c1, $c2, $c3]; asort($arr); $age = implode('-', $arr); if (!in_array($age, $posible)) { $posible[] = $age; } } } } } return $posible;}

Output:

var_dump(getAge(13));/**array (size=12)  0 => string '1-3-9' (length=5)  1 => string '1-4-8' (length=5)  2 => string '1-5-7' (length=5)  3 => string '1-6-6' (length=5)  4 => string '2-2-9' (length=5)  5 => string '2-3-8' (length=5)  6 => string '2-4-7' (length=5)  7 => string '2-5-6' (length=5)  8 => string '3-3-7' (length=5)  9 => string '3-4-6' (length=5)  10 => string '3-5-5' (length=5)  11 => string '4-4-5' (length=5)**/

The above output answer is wrong. How to answer the question?

Reply content:

Topic:

经理有三个女儿,年龄相加为13。三个女儿的年龄相乘为经理的年龄,经理的一个手下知道 经理的年龄,但是不知道其三个女儿的年龄。经理告诉手下有一个女儿头发是黑色的,手下立即知道了三个女儿的年龄。请问三个女儿的年龄分别是多少?为什么?

Calculation:


function getAge($sum){ $ageLimit = 121; // 最大年龄121岁 $ageFrist = 18; //假设最小生育年龄 18岁 $posible = []; for ($c1 = 1; $c1 <= $sum; $c1++) { for ($c2 = 1; $c2 <= $sum; $c2++) { for ($c3 = 1; $c3 <= $sum; $c3++) { if ($c1 + $c2 + $c3 == $sum && $c1 * $c2 * $c3 < $ageLimit && $c1 * $c2 * $c3 - max($c1, $c2, $c3) >= $ageFrist) { $arr = [$c1, $c2, $c3]; asort($arr); $age = implode('-', $arr); if (!in_array($age, $posible)) { $posible[] = $age; } } } } } return $posible;}

Output:

var_dump(getAge(13));/**array (size=12)  0 => string '1-3-9' (length=5)  1 => string '1-4-8' (length=5)  2 => string '1-5-7' (length=5)  3 => string '1-6-6' (length=5)  4 => string '2-2-9' (length=5)  5 => string '2-3-8' (length=5)  6 => string '2-4-7' (length=5)  7 => string '2-5-6' (length=5)  8 => string '3-3-7' (length=5)  9 => string '3-4-6' (length=5)  10 => string '3-5-5' (length=5)  11 => string '4-4-5' (length=5)**/

The above output answer is wrong. How to answer the question?

I don't always have to do this, but it's obvious that you missed a few conditions:

    1. One of the managers knows the manager's age, but he doesn't know the age of his three daughters. The manager told one of his daughters that her hair was black, and that his men knew the age of three daughters immediately. Description for the manager's age (three daughters age product), the age of the daughter has a variety of options.

    2. The manager told his men that a daughter's hair was black. "The manager told his men that only one daughter's hair was black", stating that the other two were small children, not black hair? (Logical to No? online to see ...)


  1;}, 1);//two-dimensional to one-dimensional $list = Array (); foreach ($map as $k = = $v) {$list = Array_merge ($list, $v);} Find out that only one age is greater than 2 years old (black hair)//about how many years old hair turns black, only looking for Niang $list = array_filter ($list, function ($t) {$temp = Array_filter ($t, function ($    V) {return $v > 2;    }); return count ($temp) = = 2;}); /Output if (count ($list) = = 1) {echo "found". Json_encode ($list [0]);} else {echo "not found";}? >

All outputs (the last line is the result)

1, 1, 11 = 111, 2, 10 = 201, 3, 9 = 271, 4, 8 = 321, 5, 7 = 352, 2, 9 = 361, 6, 6 = 362, 3, 8 = 482, 4, 7 = 562, 5, 6 = 603, 3, 7 = 633, 4, 6 = 723, 5, 5 = 754, 4, 5 = 80found [2,2,9,36]

My two pole PHP write too annoyed, or write JS handy, haha!

Python code

#!/usr/bin/python# -*- coding:utf-8 -*-if __name__ == '__main__':    s1 = [tuple(sorted([x, y, z])) for x in range(1,13) for y in range(1, 13) for z in range(1, 13) if x + y + z == 13 and 50 > x * y * z > 18]    s2 = set(s1)    result = [i for i in s2 if 35 > i[0] * i[1] * i[2] - max(i) > 18]    print result
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.