perm.cpp
来自「本文件为C++数据结构源代码」· C++ 代码 · 共 40 行
CPP
40 行
// output all permutations of n elements
#include <iostream.h>
#include "swap.h"
template<class T>
void Perm(T list[], int k, int m)
{// Generate all permutations of list[k:m].
int i;
if (k == m)
{// list[k:m] has one permutation, output it
// cout<<"k=m="<<k<<'\n';
cout<<"recursive end ";
for (i = 0; i <= k; i++)
cout << list[i];
cout << endl;
}
else // list[k:m] has more than one permutation
// generate these recursively
for (i = k; i <= m; i++)
{
//cout<<"recursive\n";
Swap(list[k], list[i]);
//cout<<"list="<<list<<'\n';
//cout<<"========\n";
Perm(list, k+1, m);
//ut<<"i="<<i<<" k="<<k<<" m="<<m<<'\n';
//Swap(list[k], list[i]);
}
}
void main(void)
{
char a[] = {'1', '2', '3', '4'};
int n = 3;
cout << "The permutations of 123 are" << endl;
Perm(a, 0, 2);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?