如何解决这个难题?

时间:2013-02-17 20:00:44

标签: java c++ c math puzzle

免责声明:这不是作业问题。我偶然发现了这个谜题here,我也有答案。但是,我无法找到解决问题的方法。

谜题如下:

大卫的孩子年龄的乘积是他们年龄总和的平方。大卫不到八个孩子。他的孩子都没有同龄。他的孩子都没有超过14岁。他的所有孩子都至少两岁。大卫有多少个孩子,他们的年龄是多少?

答案恰好是2,4,6,12。

请建议以编程方式解决此问题的方法。

3 个答案:

答案 0 :(得分:5)

在Python中(这不是你问的,但你更需要算法):

import operator
import itertools

possible_ages = range(2,15)

# If the list of ages passed to this function returns true, then this solves the puzzle.
def valid(ages):
    product_of_ages = reduce(operator.mul, ages, 1)
    square_of_sum_of_ages = reduce(operator.add, ages, 0) ** 2
    return product_of_ages == square_of_sum_of_ages

for number_of_children in range(1, 9):
    for ages in itertools.combinations(possible_ages, number_of_children):
        if valid(ages):
            print ages

几乎立即打印出来:

(2, 4, 6, 12)

答案 1 :(得分:3)

我使用递归方法在java中解决了它。 首先,程序打印所有组合,然后最后给出正确的组合(符合指定的标准)。

该程序立即提供输出

(2, 4, 6, 12)

正如您在问题中指定的那样。

public class Tackle {
static int[] ages = {2,3,4,5,6,7,8,9,10,11,12,13,14};   // Since the program uses a recursive function,
static StringBuffer sb = new StringBuffer("");          // the variables are declared as static
static int x=0,occurances=0;
static int sum,pdt=1,count=0;
static String[] instances = new String[100];
static void recurse(int a[], int k, int n) throws Exception
{
    if(k==n)    // This program obtains various combinations using binary technique
    {
        for(int i=0;i<n;i++)
            if(a[i] == 1){
                    System.out.print(ages[i]+" ");   // Displays all the combinations available             
                    sum = sum + ages[i];
                    pdt = pdt * ages[i];  
                    count++;                                        
                    sb.append(String.valueOf(ages[i]+" "));
                    }
                    if(Math.pow(sum, 2) == pdt && count<8){     // Checking the criteria
                        instances[occurances++] = sb.toString();
                    }

                    sb = new StringBuffer("");
                    count = 0;
                    sum = 0;
                    pdt = 1;
                    System.out.println("");
    }
    else for(int i=0;i<=1;i++)
    {       
        a[x++] = i;
        recurse(a,k+1,n);
        x--;
    }
}

public static void main(String[] args) throws Exception {
    int[] a = new int[10000];
    recurse(a,0,ages.length);
    if(occurances>0)
    {
        System.out.println("No of combinations matching: " + occurances);
        for(int i=0;i<occurances;i++)
        System.out.println("The combination of ages is [ " + instances[i] + "]");
    }
    else
        System.out.println("No combination matches the criteria. ");
}
}

获得的输出是

[All possible combinations are listed here]
No of combinations matching: 1
The combination of ages is [ 2 4 6 12 ]

答案 2 :(得分:1)

你没有指定年龄都是整数,但我会假设这是真的。如果是这样,那么8个孩子中只有大约1e9个可能的年龄组合。简单地枚举(for(age1=2; age1<15; age1++) { for(age2=2; age2<15; age2++) { ...)所有并测试。即使在脚本解释器中,您的计算机也应该在几分钟内完成该任务。

要应用优化,因为硬编码“8”循环是笨拙的,并且因为年龄列表与顺序无关(具有“4和2”的子项与“2和4”相同)但坦率地说,我认为这不值得。编写额外步骤所花费的时间将比在运行时保存的时间更长。

相关问题