给出5个数字输出所有3个数字的不同排列

来源:百度知道 编辑:UC知道 时间:2024/05/31 10:05:33
例如: 1 2 3 4 5

输出 123 124 125 234 245 .....

请用递归实现

求n个数(1....n)中k个数的组合....如:combination(5,3)
输出:543,542,541,532,531,521,432,431,421,321
#include<stdio.h>
#include <memory.h>
int pop(int *);
int push(int );
void combination(int ,int );
int *stack;
int top;
int k;
int main()
{
int n,m;
printf("Input two numbers:\n");
while( (2!=scanf("%d%*c%d",&n,&m)) )
{
fflush(stdin);
printf("Input error! Again:\n");
}
k=m;
top=-1;
stack = new int[n];
memset(stack, 0, sizeof(int)*n);
combination(n,m);
printf("\n");
delete[] stack;
}
void combination(int m,int n)
{
int temp=m;
push(temp);
while(1)
{
if(1==temp)
{
if(pop(&temp)&&stack[0]==n) break;
}
else if( push(--temp))
{
for(int i=0;i<n;i++)
printf("%d",stack[ i]);
printf(" ");