C ++程序在STL中实现Next_Permutation
STL中的next_permutation用于将[first,last]范围内的元素重新排列到下一个字典上更大的排列。排列是N的每一个!元素可以采取的可能安排。这是C++程序,用于在STL中实现Next_permutation。
算法
Begin
Define one integer array variable elements[].
Get the number of data e from the user.
Initialize the array elements[] with e number of data from the keyboard.
Sort all the array elements.
do
show(elements, e) //to display the current content of the array
while (next_permutation(elements, elements + e))
End范例程式码
#include<iostream>
#include <algorithm>
using namespace std;
void show(int a[], int n) {
for(int i = 0; i < n; i++) {
cout<<a[i]<<" ";
}
cout<<endl;
}
int main () {
int e, i;
cout<<"Enter number of elements to be inserted: ";
cin>>e;
int elements[e];
for (i = 0; i < e; i++) {
cout<<"Enter "<<i + 1<<" element: ";
cin>>elements[i];
}
sort (elements, elements + e);
cout << "The "<<e<<"! possible permutations with ";
cout<<e<<" elements: "<<endl;
do {
show(elements, e);
}
while (next_permutation(elements, elements + e));
return 0;
}输出结果
Enter number of elements to be inserted: 4 Enter 1 element: 7 Enter 2 element: 6 Enter 3 element: 2 Enter 4 element: 10 The 4! possible permutations with 4 elements: 2 6 7 10 2 6 10 7 2 7 6 10 2 7 10 6 2 10 6 7 2 10 7 6 6 2 7 10 6 2 10 7 6 7 2 10 6 7 10 2 6 10 2 7 6 10 7 2 7 2 6 10 7 2 10 6 7 6 2 10 7 6 10 2 7 10 2 6 7 10 6 2 10 2 6 7 10 2 7 6 10 6 2 7 10 6 7 2 10 7 2 6 10 7 6 2
热门推荐
3 祝福语情人节简短
9 小托班祝福语简短
10 红包祝福语简短10字
11 对战友的祝福语简短
12 六一祝福语简短网名
13 小学周年庆祝福语简短
14 简短的宝宝祝福语大全
15 蛋糕祝福语简短给老婆
16 小年对儿子祝福语简短
17 除夕祝福语简短创意牛年
18 班级群祝福语简短大全