📄 sort.cpp
字号:
/*用递归实现排序
n=1时Perm(R)=r
n>1时Perm(R)=(r1)Perm(R1),(r2)Perm(R2)... */
#include<iostream>
#include<cmath>
using namespace std;
int n=0;
template <class Type>
inline void Swap(Type &a,Type &b)
{
Type temp=a;
a=b;
b=temp;
}
template <class Type>
void Perm(Type list[],int k,int m)
{
if(k==m)
{ cout<<endl;
n++;
for(int i=0;i<=m;i++)
cout<<list[i];
}
else
{
for(int i=k;i<=m;i++)
{
Swap(list[k],list[i]);
Perm(list,k+1,m);
Swap(list[k],list[i]);
}
}
}
void main()
{
int list[10]={1,2,3,4,5,6,7,8,9,10};
Perm(list,6,9);
cout<<"总共有"<<n<<"种全排列!\n";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -