最近我回答了'如何在这里得到插入数据类型的数组的所有可能组合的许多重复问题之一。我的回答是:更有效的上市组合方式
#include <stdio.h>
int main()
{
/* String for combos to be enumerated */
char set[4] = { 'a', 'b', 'c', 'd' };
/* Length of the array */
int setLength = 4, a, b, c;
/* This will print all combos that have 1 value. E.g. A, B, C, D */
for (a = 0; a < setLength; a++)
{
printf("%c : ", set[a]);
}
/* This will give the 1st value of the combo */
for (a = 0; a < setLength; a++)
{
/* This will give the 2nd value. Resulting in combos with a length of 2 */
for (b = 0; b < setLength; b++)
{
printf("%c%c : ", set[a], set[b]);
}
}
/* 1st value */
for (a = 0; a < setLength; a++)
{
/* 2nd value */
for (b = 0; b < setLength; b++)
{
/* 3rd value */
for (c = 0; c < setLength; c++)
{
printf("%c%c%c : ", set[a], set[b], set[c]);
}
}
}
/* To continue with longer combos simply add more and more for loops */
printf("\n");
return 0;
}
然而,当我看着其他回答这个问题,他们都参与了内建的功能从语言例如C#。因此,我的问题是:我的答案是否正确或可靠,如果没有内置函数,那么这样做会更有效。
为什么不尝试一些不同的方案,看看哪一个更快呢?顺便说一句,你的程序似乎只适用于4元素集。这不符合解决方案的条件。 –
不相关,但在C中,如果您不想使用任何参数的函数,则必须明确声明它将'void'作为参数。 'int main()'和'int main(void)'意味着两件不同的事情。第一个参数的数量不确定,后者没有参数。另外,C规范说main函数应该看起来像'int main(void)'或'int main(int argc,char * argv [])','int main()'不是一个有效的'主要功能。 –
@Joachim Pileborg谢谢,总是欢迎批评,因为我对C还是很新的。 –