组合.cpp

来自「简单的全排列与组合问题的递归解法」· C++ 代码 · 共 47 行

CPP
47
字号
#include<stdio.h>
#include<string.h>

int n,m;
int res[100];
int used[100];

int output()
{
	int i;
	for(i=0;i<m;i++)
	{
		if(i>0)
			printf(" ");
		printf("%d",res[i]);
	}
	printf("\n");
	return 0;
}

int czuhe(int npos,int bth)
{
	int i;
	if(npos==m)
		output();
	else
		for(i=bth+1;i<=n;i++)
			if(!used[i])
			{
				res[npos]=i;
				used[i]=1;
				czuhe(npos+1,i);
				used[i]=0;
			}
	return 0;
}

int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		memset(used,0,sizeof(used));
		czuhe(0,0);
	}
	
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?