#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define MAX 10
//在N个数中取M个数的组合, 所有可能列出来
//利用栈结构输出组合的所有可能
typedef struct stack {
int arr[MAX];
int top; //栈顶指针
int bottom; //栈底指针
} S;
//输出栈
void print_stack(S* s, int* arr) {
int i;
for (i = 0; i <= s->top; i++) {
printf("%d", arr[s->arr[i]]);
}
printf("\n");
}
//压栈
bool push(S* s, int num, int m) {
if (s->top + 1 == m) {
return false; //满栈
}
s->top++;
s->arr[s->top] = num;
return true;
}
//出栈 当栈为空栈时 所有情况都已遍历完成
bool pop(S* s) {
if (s->top < 0) {
return false; //空栈
}
s->arr[s->top] = 0;
s->top--;
return true;
}
void initStack(S* s, int m) {
memset(s->arr, 0, sizeof(s->arr));
s->top = -1;
s->bottom = 0;
int i;
for (i = 0; i < m; i++) {
push(s, i, m);
}
}
int main() {
int n, m;
scanf("%d %d", &n, &m);
int i;
int arr[n];
for (i = 0; i < n; i++) {
arr[i] = n - i;
}
S* s = (S*) malloc(sizeof(S));
//初始化栈结构
initStack(s, m);
while (s->top > -1) {
while (push(s, s->arr[s->top] + 1, m)) {} //如何有剩余空间 全部填满
while (s->arr[s->top] < n) {
print_stack(s, arr);
s->arr[s->top]++;
}
s->arr[s->top]--;
while (s->top == m - 1 || s->arr[s->top] + m > n) {
if (!pop(s)) break;
}
if (s->top >= 0) {
s->arr[s->top]++;
}
}
return 0;
}
/*
input: 5 3
ouput:
543
542
541
532
531
521
432
431
421
321
*/