怎样用C语言从一组特定数选出几个数的所有组合,例如从10个数中选出5个数的所有组合
2个回答
展开全部
我擦,这算法有些复杂,没搞对,弄了一个小时只弄出个全排列的,你看能改改不:
#include <stdio.h>
#define NUM 10
int main ()
{
int i, j, k, l, m, n, a[NUM];
n = 0;
printf("Enter NUM numbers:\n");
for(i = 0; i < NUM; i++){
scanf("%d", &a[i]);
}
for(i = 0; i < NUM; i++){
for(j = 0; j < NUM; j++){
if(i == j){
continue;
}
for(k = 0; k < NUM; k++){
if(k == i || k ==j){
continue;
}
for(l = 0; l < NUM; l++){
if(l == i || l == j || l == k){
continue;
}
for(m = 0; m < NUM; m++){
if(m == i || m == j || m == k || m == l){
continue;
}
printf("%d,%d,%d,%d,%d\n", a[i], a[j], a[k], a
[l], a[m]);
n++;
}
}
}
}
}
printf("Counts:%d\n", n);
return 0;
}
#include <stdio.h>
#define NUM 10
int main ()
{
int i, j, k, l, m, n, a[NUM];
n = 0;
printf("Enter NUM numbers:\n");
for(i = 0; i < NUM; i++){
scanf("%d", &a[i]);
}
for(i = 0; i < NUM; i++){
for(j = 0; j < NUM; j++){
if(i == j){
continue;
}
for(k = 0; k < NUM; k++){
if(k == i || k ==j){
continue;
}
for(l = 0; l < NUM; l++){
if(l == i || l == j || l == k){
continue;
}
for(m = 0; m < NUM; m++){
if(m == i || m == j || m == k || m == l){
continue;
}
printf("%d,%d,%d,%d,%d\n", a[i], a[j], a[k], a
[l], a[m]);
n++;
}
}
}
}
}
printf("Counts:%d\n", n);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询