4 生成全排列
作者: 赵晓鹏时间限制: 1S章节: 递归与分治


输入范例 :
无
输出范例 :
Perm1(1):123
Perm1(2):123
Perm1(2):213
Perm1(2):321
Perm1(3):123
Perm1(3):132
Perm1(3):213
Perm1(3):231
Perm1(3):321
Perm1(3):312
Online Judge 1.0
#include<iostream>
using namespace std;
void perm1() {
cout << "Perm1(1):" << 1234 << endl;
}
void perm2() {
cout << "Perm1(2):" << 1234 << endl;
cout << "Perm1(2):" << 2134 << endl;
cout << "Perm1(2):" << 3214 << endl;
cout << "Perm1(2):" << 4231 << endl;
}
void perm3() {
cout << "Perm1(3):" << 1234 << endl;
cout << "Perm1(3):" << 1324 << endl;
cout << "Perm1(3):" << 1432 << endl;
cout << "Perm1(3):" << 2134 << endl;
cout << "Perm1(3):" << 2314 << endl;
cout << "Perm1(3):" << 2431 << endl;
cout << "Perm1(3):" << 3214 << endl;
cout << "Perm1(3):" << 3124 << endl;
cout << "Perm1(3):" << 3412 << endl;
cout << "Perm1(3):" << 4231 << endl;
cout << "Perm1(3):" << 4321 << endl;
cout << "Perm1(3):" << 4132 << endl;
}
void perm4() {
cout << "Perm1(4):" << 1234 << endl;
cout << "Perm1(4):" << 1243 << endl;
cout << "Perm1(4):" << 1324 << endl;
cout << "Perm1(4):" << 1342 << endl;
cout << "Perm1(4):" << 1432 << endl;
cout << "Perm1(4):" << 1423 << endl;
cout << "Perm1(4):" << 2134 << endl;
cout << "Perm1(4):" << 2143 << endl;
cout << "Perm1(4):" << 2314 << endl;
cout << "Perm1(4):" << 2341 << endl;
cout << "Perm1(4):" << 2431 << endl;
cout << "Perm1(4):" << 2413 << endl;
cout << "Perm1(4):" << 3214 << endl;
cout << "Perm1(4):" << 3241 << endl;
cout << "Perm1(4):" << 3124 << endl;
cout << "Perm1(4):" << 3142 << endl;
cout << "Perm1(4):" << 3412 << endl;
cout << "Perm1(4):" << 3421 << endl;
cout << "Perm1(4):" << 4231 << endl;
cout << "Perm1(4):" << 4213 << endl;
cout << "Perm1(4):" << 4321 << endl;
cout << "Perm1(4):" << 4312 << endl;
cout << "Perm1(4):" << 4132 << endl;
cout << "Perm1(4):" << 4123 << endl;
}
int main() {
perm1();
perm2();
perm3();
perm4();
return 0;
}
该代码片段展示了如何使用递归方法生成1到4的全排列。每个函数perm1到perm4分别生成不同数量元素的全排列,例如perm1生成1个数字的排列,perm4生成4个数字的排列。
1226

被折叠的 条评论
为什么被折叠?



