发现的总数组合使用三个数的整数组合、整数、个数、总数

由网友(人心凉薄)分享简介:对于一个给定的整数, N 的,我需要打印长度为3的所有列出了总和的 N 的。所述列表的成员必须为非负整数。打印所有这些名单后,必须再打印中发现,清单的数量。For a given integer, n, I need to print all the lists of length 3 which sum to n....

对于一个给定的整数, N 的,我需要打印长度为3的所有列出了总和的 N 的。所述列表的成员必须为非负整数。打印所有这些名单后,必须再打印中发现,清单的数量。

For a given integer, n, I need to print all the lists of length 3 which sum to n. The members of the list must be non-negative integers. After printing all these lists, it must then print the number of lists that were found.

例如,如果的 N = 2 的:

1 + 0 + 1 = 2 1 + 1 + 0 = 2 0 + 1 + 1 = 2 2 + 0 + 0 = 2 0 + 0 + 2 = 2 0 + 2 + 0 = 2

下面是我为长度为2的列表,而不是长度3列出了程序:

Here is the program I did for lists of length 2 rather than lists of length 3:

#include <stdio.h>
int main (void)
{
    int total;
    int c1=0;
    int c2=0;
    int c3=0;
    int count;

    printf("Welcome friends and mainly enemies to the thingy. Only postive intergers!!!n That's right just enter a number here:");
    scanf ("%d",&total);
    printf ("nnnWhy pick %d? Here is the list if combinations anyway,",total);

    for (count=0;count<=total;count++)
    {
        printf ("n");
        for (c1=count;c1==count;c1--)
        {
            printf("%d ",c1);

        }
        for (c2=total-count;c2<=total-count;c2++)
        {
            printf("%d",c2);

        }

    }
    printf ("nnThere are %d number combinations that total %d",count,total);

}

的目标是从长度的两个列表扩展这对长度为3的列表。

The goal is to extend this from lists of length two to lists of length 3.

附加信息: 我只能用另外一个变量C3。

Additional Info: I can only use one other variable c3.

推荐答案

希望这有助于:

int c3 = 0;
for (int c1 = 0; c1 <= total; c1++) {       
    for (int c2 = total - c1; c2 >= 0; c2--){
        printf("%d %d %d n", c1, c2, total - c1 - c2);
        c3++;
    }

}

printf("there are %d ways to sum to %d", c3, total);
阅读全文

相关推荐

最新文章