日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

下一个全排列_下一个排列

發(fā)布時間:2025/3/11 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 下一个全排列_下一个排列 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

下一個全排列

Problem statement:

問題陳述:

Given a permutation print permutation just greater than this.

給定一個排列,打印排列就比這個更大。

Example:

例:

Permutation:1 3 2 5 4Output:1 3 4 2 5

Solution:

解:

What is permutation?

什么是排列?

Permutation is the process of arranging the members of a set into a sequence or order, or, if the set is already ordered, rearranging (reordering) its elements. (Ref. wiki: Permutation)

置換是將集合的成員排列為序列或順序的過程,或者,如果已經(jīng)對集合進行了排序,則重新排列 (重新排序)其元素。 (參考維基:置換 )

Example:

例:

Let a set s= {1, 2, 3}Then it has six permutations which are(1, 2, 3)(1, 3, 2)(2, 1, 3)(2, 3, 1)(3, 1, 2)(3, 2, 1)This all are ordered Thus the next permutation of (1, 3, 2) is (2, 1, 3) and next of (3, 2, 1) is (1, 2, 3). (Yah! It's cyclic)

Generating Next permutation

產(chǎn)生下一個排列

This problem has a simple but robust algorithm which handles even repeating occurrences. However for this problem we restrict our discussion to single occurrence of numbers in the permutation.

這個問題有一個簡單但健壯的算法,可以處理重復(fù)的事件。 但是,對于這個問題,我們將討論限制為排列中數(shù)字的單一出現(xiàn)。

Pre-requisite:

先決條件:

Input permutation of length n

長度為n的輸入排列

Algorithm:

算法:

1. Find the largest k such that a[k]<a[k+1] , k? [0, n-1] //k=-1 initially 2. IFk is not updated (k is -1 still) that means current is the largest permutation (descending order).So the next will be the smallest one (ascending order).ELSE 3. Find largest l such that a[k]<a[l] 4. Swap a[k] and a[l] 5. Reverse a[k+1] to a[n-1]

Example with Explanation:

解釋示例:

K initialized to be -1Example1: Permutation: 1 3 2 5 4 k=2 //a[k]=2 l=4 //a[l]>a[k] i.e. 4>2 After swapping a[k] and a[l] Permutation 1 3 4 5 2 Reversing a[k+1] to a[n-1] 1 3 4 2 5 //output Example 2: Permutation: 5 4 3 2 1 //largest in the possible permutations k=1 //since no such kfound, k is not updated Sort the current permutation in ascending order Next Permutation 1 2 3 4 5 //output .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

C++ implementation

C ++實現(xiàn)

#include <bits/stdc++.h> using namespace std;//to print a vector void print(vector<int> a,int n){ for(int i=0;i<n;i++)cout<<a[i]<<" ";cout<<endl; }void nextPermutation(vector<int> a,int n){int k=-1,l,temp;//finding largest k s.t. a[k]<a[k+1]for(int i=0;i<n-1;i++){if(a[i]<a[i+1])k=i;}//if k not updated sort and printif(k==-1){sort(a.begin(),a.end());print(a,n);return;}//find the largest l s.t. a[k]<a[l]for(int i=k+1;i<n;i++){if(a[i]>a[k])l=i;}//swap a[k] and a[l]temp=a[k];a[k]=a[l];a[l]=temp;//print upto a[k]for(int i=0;i<=k;i++)cout<<a[i]<<" ";//reverse printing for a[k+1] to a[n-1] for(int i=n-1;i>k;i--)cout<<a[i]<<" ";cout<<endl;return; }int main(){int n,item;cout<<"enter length of permutation\n";scanf("%d",&n);cout<<"enter the permutation leaving spaces betweeen two number\n";vector<int> a; for(int j=0;j<n;j++){scanf("%d",&item);a.push_back(item);}cout<<"Current permutation is:\n";print(a,n);cout<<"next permutation is:\n";nextPermutation(a,n);return 0; }

Output

輸出量

First run: enter length of permutation 5 enter the permutation leaving spaces betweeen two number 1 3 2 5 4 Current permutation is: 1 3 2 5 4 next permutation is: 1 3 4 2 5Second run: enter length of permutation 5 enter the permutation leaving spaces betweeen two number 5 4 3 2 1 Current permutation is: 5 4 3 2 1 next permutation is: 1 2 3 4 5

翻譯自: https://www.includehelp.com/icp/next-permutation.aspx

下一個全排列

總結(jié)

以上是生活随笔為你收集整理的下一个全排列_下一个排列的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。