Code for sorting and combination

Source: Internet
Author: User
1. Generate class cyclic Arrangement
Input example
3 2
Output example
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
# Include <iostream> # include <ctime> using namespace std; int A [100]; void Print_permutation (int n, int * A, int cur) {int I, j; if (cur = n) {/* for (I = 0; I <n; ++ I) {cout <A [I] <"";} cout <endl; */} else {for (I = 1; I <= n; I ++) {bool flag = true; for (j = 0; j <cur; ++ j) {if (A [j] = I) {flag = false; break ;}} if (flag) {A [cur] = I; print_permutation (n, A, cur + 1) ;}}int main () {int n = 0; clock_t start, end; cin> n; start = clock (); Print_permutation (n, A, 0); end = clock (); cout <endl <(double) (end-start) /CLOCKS_PER_SEC <"s" <endl; return 0;} In fact, This method uses recursion to implement multiple loops. This recursive program is equivalent to n reloops, each
When the length of the recycle is m, the output contains a total of m ^ n rows.

 

 

2. Do not repeat:

The number of n is input, and the number of n is arranged. Repeated items are not allowed.
Input example
3
1 1 2
Output example
1 1 2
1 2 1
2 1 1

Version One : 
LRJ code:
#include <iostream> using namespace std ; int P[100] , A[100] ; void Print_permutation(int n , int *P , int *A , int cur) { if (cur == n) { for (int i = 0 ; i < n ; ++ i) { cout << A[i] << " " ; } cout << endl ; } else { for (int i = 0 ; i < n ; ++ i) { if (!i || P[i] != P[i-1]) { int c1 = 0 , c2 = 0 ; for (int j = 0 ; j < cur ; ++ j) { if (A[j] == P[i]) { c1 ++ ; } } for (int j = 0 ; j < n ; ++ j) { if (P[i] == P[j]) { c2 ++ ; } } if (c1 < c2) { A[cur] = P[i] ; Print_permutation(n,P,A,cur+1) ; } } } } } int main() { int n ; cin >> n ;  for (int i = 0 ; i < n ; ++ i) { cin >> P[i] ; } Print_permutation(n,P,A,0) ; return 0 ; } 
 
Version Two :
#include <iostream> using namespace std ; const int MAXN = 100 ; int A[MAXN] , P[MAXN] , used[MAXN] , tmp[MAXN] ;  void Print_permutation(int n , int m , int *A , int *P , int cur) {if (cur == n) { for (int i = 0 ; i < n ; ++ i) { cout << A[i] << " " ; } cout << endl ; } else { for (int i = 0 ; i < m ; ++ i) { if (used[i] > -1) { used[i] -- ; A[cur] = P[i] ; Print_permutation(n,m,A,P,cur+1) ; used[i] ++ ; } } } }    int main() { int n , m ; cin >> n ; int count = 0 ; memset(P,0,sizeof(P)) ; for(int i = 0 ; i < n ; ++ i) {cin >> tmp[i] ; P[count++] = tmp[i] ; for (int j = 0 ; j < i ; ++ j) { if (tmp[j] == tmp[i]) { used[j] ++ ; count -- ; break ; } } } for (int i = 0 ; i < count ; ++ i) { cout << P[i] << " " ; } cout << endl ; Print_permutation(n,count,A,P,0) ; return 0 ; } 

Version Three:

(Core Implementation of next_permutation in STL)

template<class _BidIt> inline
 bool next_permutation(_BidIt _First, _BidIt _Last)
 { // permute and test for pure ascending, using operator<
 _DEBUG_RANGE(_First, _Last);
 return (_Next_permutation(_Unchecked(_First), _Unchecked(_Last)));
 }
template<class _BidIt> inlinebool _Next_permutation(_BidIt _First, _BidIt _Last){// permute and test for pure ascending, using operator<_BidIt _Next = _Last;if (_First == _Last || _First == --_Next)return (false);for (; ; ){// find rightmost element smaller than successor_BidIt _Next1 = _Next;if (_DEBUG_LT(*--_Next, *_Next1)){// swap with rightmost element that's smaller, flip suffix_BidIt _Mid = _Last;for (; !_DEBUG_LT(*_Next, *--_Mid); );_STD iter_swap(_Next, _Mid);_STD reverse(_Next1, _Last);return (true);}if (_Next == _First){// pure descending, flip all_STD reverse(_First, _Last);return (false);}}}

3. Full arrangement: (input without repeated elements)

#include <iostream> using namespace std ; const int MAXN = 100 ; int used[MAXN] , A[MAXN] , P[MAXN] ; void Print_permutation(int n , int *A , int *P , int cur) { if (cur == n) { for (int i = 0 ; i < n ; ++ i) { cout << A[i] << " " ; } cout << endl ; } else { for (int i = 0 ; i < n ; ++ i) { if (!used[i]) { used[i] = 1 ; A[cur] = P[i] ; Print_permutation(n,A,P,cur+1) ; used[i] = 0 ; } } }} int main() { int n = 0 ; cin >> n ; for (int i = 0 ; i < n ; ++ i) { cin >> P[i] ; } memset(used,0,sizeof(used)) ; Print_permutation(n,A,P,0) ; return 0 ; } 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.