实现Johnson-Trotter算法
本篇文章只是作为自己学习的记录文章,参考了前人的代码,在前人代码的基础上,添加了自己的理解和注释,建议先看一下参考
参考链接
#include <iostream>
using namespace std;
//建立结构体来储存
struct node{
int num;//表示全排列中的每个数字
bool flag;//表示箭头,向左为假,向右为真
};
void JohnsonTrotter(int n){
//n是全排列数字的个数
node* a = new node[n+1];//建立数组存储数列,多一位是因为从角标1开始
int i;
//给数组赋值
for(i=0; i<=n; i++)
{
a[i].num = i;
a