Interview question-PHP algorithm logic: how to calculate age?

Source: Internet
Author: User
Question: {code...} calculation: {code...} output: {code...} The above output is incorrect. How to answer this question? Question:

The manager has three daughters, aged 13. The age of the three daughters is multiplied by the manager's age. One manager knows the manager's age, but does not know the age of the three daughters. The manager told his daughter that his hair was black and he immediately knew the age of the three daughters. What is the age of the three daughters? Why?

Computing:


Function getAge ($ sum) {$ ageLimit = 121; // maximum age: 121 years old $ ageFrist = 18; // assume the minimum childbearing age is 18 years old $ 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 is incorrect. How to answer this question?

Reply content:

Question:

The manager has three daughters, aged 13. The age of the three daughters is multiplied by the manager's age. One manager knows the manager's age, but does not know the age of the three daughters. The manager told his daughter that his hair was black and he immediately knew the age of the three daughters. What is the age of the three daughters? Why?

Computing:


Function getAge ($ sum) {$ ageLimit = 121; // maximum age: 121 years old $ ageFrist = 18; // assume the minimum childbearing age is 18 years old $ 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 is incorrect. How to answer this question?

I have never done this kind of question, but it is too obvious that you have missed several conditions:

  1. One manager knows the manager's age, but does not know the age of the three daughters. The manager told his daughter that his hair was black and he immediately knew the age of the three daughters. -> There are multiple options for the age of a manager (the product of three daughters.

  2. The manager told his daughter that his hair was black-> it should be "the manager told him that only one of her daughters had a black hair". What does it mean that the other two are little children and their hair is not black? (Logical right? On the internet ...)


  1 ;}, 1); // convert two dimensions to one dimension $ list = array (); foreach ($ map as $ k =>v v) {$ list = array_merge ($ list, $ v) ;}// find out that only one of the ages is older than 2 years old (black hair) // For how many years old hair turns black, only find du Niang $ list = array_filter ($ list, function ($ t) {$ temp = array_filter ($ t, function ($ v) {return $ v> 2 ;}); return count ($ temp) = 2;}); // output result if (count ($ list) = 1) {echo "found ". json_encode ($ list [0]);} else {echo "not found" ;}?>

All outputs (the last row 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 second-pole PHP is too annoying to write. I still write JS, 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

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.