-2

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

谜题如下:

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

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

请提出一种以编程方式解决此问题的方法。

4

3 回答 3

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)
于 2013-02-17T21:00:29.667 回答
1

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

有一些优化可以应用,因为硬编码“8”循环很笨拙,而且因为年龄列表是顺序无关的(有“4 和 2”的孩子与“2 和 4”是一样的),但坦率地说我认为这里不值得。您编写额外步骤所花费的时间将比您在运行时节省的时间更多。

于 2013-02-17T20:45:55.237 回答
0

我使用递归方法在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 ]
于 2013-02-17T21:16:52.963 回答