求排列

#include"iostream.h"
int str[5]={0,1,2,3,4};
void swap(int &a,int &b);
void perm(int list[5],int k,int m)
{
	if(k==m)
	{
		for(int i=0;i<m;i++)
			cout<<list[i];
		cout<<endl;
	}
	else
		for(int i=k;i<m;i++)
		{
			swap(list[k],list[i]);
			perm(list,k+1,m);
			swap(list[k],list[i]);
		}
}
void swap(int &a,int &b)
{
	int t=a;a=b,b=t;
}
void main()
{
	perm(str,0,3);
}
運行結果:
012
021
102
120
210
201
Press any key to continue
 
發佈了39 篇原創文章 · 獲贊 2 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章