多种方法实现实现全排列 + sort调用标准函数库函数的简述
发布日期:2021-05-13 00:44:49 浏览次数:15 分类:博客文章

本文共 10043 字,大约阅读时间需要 33 分钟。

������������������������������������������������������������������������������������������������������������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������������������1���2���������������������������������������1���������������������������������������������������������������������������2������������������������������������������������2������������������������������������������������1������

������������������1���2���3���������������������������������������������������������������1���2���3������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������������������������������������������������������n���������������������������������������������n-1������������������������������������������������������n-1���������������������������������������������n-2���������������������(...)������

���������������������������������������������������������

1 #include 
2 3 int cnt = 0; // ������������������������������������������������������������ 4 5 void swap(int *a, int *b) { 6 int m; 7 m = *a; 8 *a = *b; 9 *b = m; 10 }11 12 void perm(int list[], int k, int m) { 13 int i; 14 if(k > m) { 15 for(i = 0; i <= m; i++) 16 printf("%d ", list[i]); 17 printf("\n"); 18 cnt++;19 } else {20 for(i = k; i <= m; i++) { 21 swap(&list[k], &list[i]); 22 perm(list, k + 1, m); 23 swap(&list[k], &list[i]); 24 } 25 } 26 }27 28 int main() {29 int list[] = {1, 2, 3, 4, 5}; // ���������������������������������������������������30 perm(list, 0, 4);31 printf("total:%d\n", cnt);32 return 0; 33 }34 35 // ������������C++������������������������������������36 // ������������������������������STL���������������������������
���������-C������������������

 ������������������������������1���2���3���4���������������������������������������������������������������������������1���������������������

������1 2 3 4

������1 2 4 3
������1 3 2 4
������1 3 4 2
������1 4 3 2
������1 4 2 3

������ ���������������������������������������������������������������������������������������1~6������������������������������������������������������������������

���  ������������������������������������������������������������������������������������������������������������������������������������������:

���������������������������������������������������������1���������������������������������������2������������������������������������3������������������������4���������1 2 3 4���

������������������������������������������������4������������������������3������������������������������������������������������������������1 2 4 3���

���������������������������������������������3������������������������������2���������������2 3���������������������������������������4���������1 3 2 4���

���������������������������������������������4���������������������������������������������������������2���������1 3 4 2���

���������������... ������������������������4������������2������������4���������������������������������������������1 4 3 2���

������������������������������������������1 4 2 3���

���������������������������������������������2���������������������������

���������������������������������������������������������������������������������������4������<������3>���������2���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������������������������������������������������������������������������������������������������������������������������������������0123���������������������������0123������������������������������2���������������3,3���������������2,���������������������������

 

1 #include 
2 #include
3 #include
4 using namespace std; 5 6 bool used[100]; 7 int p[100]; 8 9 void perm (int pos, int n) {10 if (pos == n) {11 for (int i = 0; i < n; ++i) {12 cout << p[i] << " ";13 }14 cout << endl;15 return;16 }17 for (int i = 0; i < n; ++i) {18 if (!used[i]) {19 p[pos] = i;20 // i���������������������������������������true21 used[i] = true;22 perm(pos+1, n);23 // ������������������������������������24 used[i] = false;25 }26 }27 return;28 }29 30 int main () {31 int n;32 cin >> n;33 perm(0, n);34 return 0;35 }
���������������������������������������������

 

���������������������C++������STL���������������������������������������������������������������������������������������������C++���������������STL������������������������������

��� ������������������C++������������sort()--���������������next_permutation(); ��� prev_permutation();������������������������������������������������������������������������������������������������������������������������������������1234���������������������1243���������������������1423������������������������1432������������������������������������������������������������������������123456���������������������654321������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������next_permutation()������������������������

������bool next_permutation( iterator start, iterator end );

���������������������������������������������������������iterator������������������������bool���

1 #include 
2 #include
3 #include
4 using namespace std; 5 6 int main() { 7 string str; 8 cin >> str; 9 sort(str.begin(), str.end()); // ���������������������������������������������������������10 while (next_permutation(str.begin(), str.end())) 11 cout << str << endl;12 return 0;13 }
C++���������������STL���������������

������������������������������������������C++���������������������������������������������C++������C������������������������������������������������������������������������������������������������C++������������C������������������������������������������������������������������������������C++���C������������������������������������������

1 #include 
2 #include
3 #include
4 using namespace std; 5 6 const long long MAX = 1e13 + 10; 7 8 char str[MAX]; 9 10 int main()11 {12 int length;13 gets(str);14 length = strlen(str);15 sort(str, str + length);16 puts(str);17 while (next_permutation(str, str + length))18 {19 puts(str);20 }21 return 0;22 }
C++���C���������������������������

 

������������������������������������������next_permutation()������������������������sort���������sort������������������������������������prev_permutation()���������������������sort������������������������������������������������������������������������������������������������������������sort���������������������������������sort������������������������������������������������������������������������������������

1 #include 
2 #include
3 using namespace std; 4 5 bool compare(int a, int b) { 6 return a < b; //���������������������������return a>b��������������� 7 } 8 9 int main() {10 int a[10000], n;11 cin >> n;12 for(int i = 0; i < n; i++)13 cin >> a[i];14 15 sort(a, a+n, compare);16 17 for(int i = 0; i < n; i++)18 cout << a[i] << endl;19 return 0;20 }
sort������������������

������sort���������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������

 

1 #include 
2 #include
3 using namespace std; 4 5 int main () { 6 string str; 7 cin >> str; 8 sort (str.begin(), str.end(), [](const char a, const char b){return a > b;}); 9 cout << str << endl;10 return 0;11 }
������������sort���������������������

 

���������������������������������������������g++������������������������������������������-std=c++11

������������ g++ -std=c++11 Test.cpp -o a&&a

������������������������C++���������������������������������less<������������>()��� // ���������������������greater<������������>() //���������������������

1 #include 
2 #include
3 using namespace std; 4 5 bool compare (int a, int b) { 6 return a > b; 7 } 8 int main () { 9 int a[] = {1, 2, 3, 4, 5};10 sort (a, a+5, greater
()); // ���������������������������less
()������������������11 for (int i = 0; i < 5; ++i) {12 cout << a[i] << " ";13 }14 cout << endl;15 16 while (prev_permutation(a, a+5)) {17 for (int i = 0; i < 5; ++i) {18 cout << a[i] << " ";19 }20 cout << endl;21 }22 return 0;23 }
������less���greater������������������

���������������������������������less���greater������string���������������������������������string���������������������������������������������������������������������������������char������int������������������������������������������������������������������������������������������������������������������

 

 

������

 

上一篇:hdu - 1716 排列2 (使用set对全排列结果去重)
下一篇:HDU - 2091 空心三角形 水题,但是有点坑...

发表评论

最新留言

感谢大佬
[***.8.128.20]2025年05月03日 17时54分02秒