# C语言中，输出一组数的排列和组合

## N个元素中取出M个元素的所有排列

#include <stdio.h>

#define MAX 10

void print();
void Perm(int step);

int used[MAX] = {0};
int result[MAX] = {0};
int M, N;

int main()
{
printf("Please input N M = ");
scanf("%d %d", &N, &M);
printf("N = %d, M = %d\n", N, M);
Perm(0);
return 0;
}
void print()
{
int i;
for(i = 0; i < M; i++)
{
printf("%d ", result[i]);
}
printf("\n");
}
void Perm(int step)
{
int i;
if (step == M)
{
print();
}
else
{
for(i = 0; i < N; i++)
{
if (!used[i])
{
used[i] = 1;
result[step] = i + 1;
Perm(step + 1);
used[i] = 0;
}
}
}
}

## N个元素中取出M个元素的所有组合

#include <stdio.h>

#define MAX 20

int c[MAX] = {0};
int M, N;

void print();
void comp(int);

int main()
{
printf("Please input N M = ");
scanf("%d %d", &N, &M);
printf("N = %d, M = %d\n", N, M);

comp(1);

return 0;
}
void print()
{
int i;
for (i = 0; i < M; i++)
{
printf("%d", c[i + 1]);
}
printf("\n");
}
void comp(int m)
{
if (m == M + 1)
{
print();
}
else
{
for (c[m] = c[m - 1] + 1; c[m] <= N - M + m; c[m]++)
{
comp(m + 1);
}
}
}