sort.cpp
来自「本程序实现了利用递归排序实现了数组的全排列,通过用户输入数组和选择排列的起始位置」· C++ 代码 · 共 41 行
CPP
41 行
/*用递归实现排序
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 + =
减小字号Ctrl + -
显示快捷键?