想问一道数学题.1到11个数字里面加起来等于30的有几个组合.每次必须是5个数字相加,和值为30的有几个,算法说一下.1

1个回答

  • 一共 32 种组合,我写代码遍历了一下.所有组合如下:

    1 + 2 + 6 + 10 + 11 = 30

    1 + 2 + 7 + 9 + 11 = 30

    1 + 2 + 8 + 9 + 10 = 30

    1 + 3 + 5 + 10 + 11 = 30

    1 + 3 + 6 + 9 + 11 = 30

    1 + 3 + 7 + 8 + 11 = 30

    1 + 3 + 7 + 9 + 10 = 30

    1 + 4 + 5 + 9 + 11 = 30

    1 + 4 + 6 + 8 + 11 = 30

    1 + 4 + 6 + 9 + 10 = 30

    1 + 4 + 7 + 8 + 10 = 30

    1 + 5 + 6 + 7 + 11 = 30

    1 + 5 + 6 + 8 + 10 = 30

    1 + 5 + 7 + 8 + 9 = 30

    2 + 3 + 4 + 10 + 11 = 30

    2 + 3 + 5 + 9 + 11 = 30

    2 + 3 + 6 + 8 + 11 = 30

    2 + 3 + 6 + 9 + 10 = 30

    2 + 3 + 7 + 8 + 10 = 30

    2 + 4 + 5 + 8 + 11 = 30

    2 + 4 + 5 + 9 + 10 = 30

    2 + 4 + 6 + 7 + 11 = 30

    2 + 4 + 6 + 8 + 10 = 30

    2 + 4 + 7 + 8 + 9 = 30

    2 + 5 + 6 + 7 + 10 = 30

    2 + 5 + 6 + 8 + 9 = 30

    3 + 4 + 5 + 7 + 11 = 30

    3 + 4 + 5 + 8 + 10 = 30

    3 + 4 + 6 + 7 + 10 = 30

    3 + 4 + 6 + 8 + 9 = 30

    3 + 5 + 6 + 7 + 9 = 30

    4 + 5 + 6 + 7 + 8 = 30

    对于 N = 29,也一共有 32 种组合,所有组合如下:

    1 + 2 + 5 + 10 + 11 = 29

    1 + 2 + 6 + 9 + 11 = 29

    1 + 2 + 7 + 8 + 11 = 29

    1 + 2 + 7 + 9 + 10 = 29

    1 + 3 + 4 + 10 + 11 = 29

    1 + 3 + 5 + 9 + 11 = 29

    1 + 3 + 6 + 8 + 11 = 29

    1 + 3 + 6 + 9 + 10 = 29

    1 + 3 + 7 + 8 + 10 = 29

    1 + 4 + 5 + 8 + 11 = 29

    1 + 4 + 5 + 9 + 10 = 29

    1 + 4 + 6 + 7 + 11 = 29

    1 + 4 + 6 + 8 + 10 = 29

    1 + 4 + 7 + 8 + 9 = 29

    1 + 5 + 6 + 7 + 10 = 29

    1 + 5 + 6 + 8 + 9 = 29

    2 + 3 + 4 + 9 + 11 = 29

    2 + 3 + 5 + 8 + 11 = 29

    2 + 3 + 5 + 9 + 10 = 29

    2 + 3 + 6 + 7 + 11 = 29

    2 + 3 + 6 + 8 + 10 = 29

    2 + 3 + 7 + 8 + 9 = 29

    2 + 4 + 5 + 7 + 11 = 29

    2 + 4 + 5 + 8 + 10 = 29

    2 + 4 + 6 + 7 + 10 = 29

    2 + 4 + 6 + 8 + 9 = 29

    2 + 5 + 6 + 7 + 9 = 29

    3 + 4 + 5 + 6 + 11 = 29

    3 + 4 + 5 + 7 + 10 = 29

    3 + 4 + 5 + 8 + 9 = 29

    3 + 4 + 6 + 7 + 9 = 29

    3 + 5 + 6 + 7 + 8 = 29

    目前我没找到啥不用遍历的好方法,做一些简单的分类讨论虽然能节省一点计算,但仍然需要比较多的枚举.