【发布时间】:2012-01-15 16:16:27
【问题描述】:
对于给定的整数 n,我需要打印所有长度为 3 且总和为 n 的列表。列表的成员必须是非负整数。打印完所有这些列表后,它必须打印找到的列表数。
例如,如果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 的列表所做的程序:
#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 ("\n\n\nWhy 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 ("\n\nThere are %d number combinations that total %d",count,total);
}
目标是将其从长度为 2 的列表扩展到长度为 3 的列表。
附加信息: 我只能使用另一个变量 c3。
【问题讨论】:
-
问题是我该怎么做。
-
你的意思是“给定一个整数 n,打印所有长度为 m 且总和为 n 的列表。每个列表的成员都是非负整数。”
-
是的,我相信这就是我的意思。如果按长度列表表示所有组合