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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++STL之next_permutation使用

發布時間:2025/4/5 c/c++ 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++STL之next_permutation使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這是一個計算排列組合的函數
next_permutaion 下一個排列
生成一個字典序更大的排列

Rearranges the elements in the range [first,last) into the next lexicographically greater permutation.

返回值是bool型
如果仍然可以找到字典序更大的排列,則返回true,并且數組(假定是數組)中變成一個新的排列,比如{2,1,3};如果不能再找到一個字典序更大的排列就返回false,而且數組還是最小的那個排列,比如{1,2,3}。

If the function can determine the next higher permutation, it rearranges the elements as such and returns true. If that was not possible (because it is already at the largest possible permutation), it rearranges the elements according to the first permutation (sorted in ascending order) and returns false.

Return value
true if the function could rearrange the object as a lexicographicaly greater permutation.
Otherwise, the function returns false to indicate that the arrangement is not greater than the previous, but the lowest possible (sorted in ascending order).

resource:https://www.cplusplus.com/reference/algorithm/next_permutation/?kw=next_permutation

Cplusplus官網給出的例程

// next_permutation example #include <iostream> // std::cout #include <algorithm> // std::next_permutation, std::sortint main () {int myints[] = {1,2,3};std::sort (myints,myints+3);std::cout << "The 3! possible permutations with 3 elements:\n";do {std::cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';} while ( std::next_permutation(myints,myints+3) );std::cout << "After loop: " << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';return 0; }

輸出結果

The 3! possible permutations with 3 elements:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
After loop: 1 2 3

分析
這里使用next_permutation之前使用sort函數對數組進行排序,以便返回所有的排列。

Leetcode46. 全排列
題目鏈接:https://leetcode-cn.com/problems/permutations/

給定一個 沒有重復 數字的序列,返回其所有可能的全排列。

示例:

輸入: [1,2,3]
輸出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]

分析
由于沒有重復數字,這樣的排列可以直接使用STL中的next_permutation函數,輸出是按照升序輸出。
同樣需要注意的是:這里使用next_permutation之前使用sort函數對數組進行排序,以便返回所有的排列。
如果next_permutation 為真,則把此時的nums數組中的元素添加到tmp數組中,直到找到所有的排列。
AC代碼

class Solution { public:vector<vector<int>> permute(vector<int>& nums) {if(nums.size()==0) return {};sort(nums.begin(),nums.end());//從小到大排序vector<vector<int>> tmp;//結果數組do{tmp.push_back(nums); //添加到結果數組中 }while(next_permutation(nums.begin(),nums.end()));//如果還有字典序更大的排列的話return tmp;} };

一道題目
1、題目描述

Ray又對數字的列產生了興趣:

現有四張卡片,用這四張卡片能排列出很多不同的4位數,要求按從小到大的順序輸出這些4位數。

輸入

輸入一組數據,包含四個整數,代表四張卡片上的數字(0<=數字<=9)

輸出

按從小到大的順序輸出所有能由這四張卡片組成的4位數,千位數字相同的在同一行,同一行中每個四位數間用空格分隔。(注意:行末不得有多余的空格和換行)并且不能有前導零

樣例輸入

1 1 2 3

樣例輸出

1123 1132 1213 1231 1312 1321

2113 2131 2311

3112 3121 3211

分析
使用一個tmp來保存最高位,方便換行。
另外需要一個cnt來判斷是否是每一行的第一個排列。

#include<iostream> #include<deque> #include<algorithm> using namespace std; int a[10];int main(){for(int i=0;i<4;i++)cin>>a[i];//全零直接輸出即可。sort(a,a+4);//沒有前導零的處理 if(a[0]==0){if(a[1]!=0) swap(a[1],a[0]);else {if(a[2]!=0) swap(a[0],a[2]);else if(a[3]!=0) swap(a[0],a[3]);}}int cnt=1;//第幾個數字 int tmp=a[0];//最高位是否相等 int num=0;//統計沒有前導零的排列的個數 do{num++;if(tmp!=a[0]){//換行 cout<<endl;// cout<<"上一行的排列個數:"<<cnt-1<<endl; cnt=1;tmp=a[0];//更新tmp ,a[0]跑的快 }if(cnt==1){//每行第一個排列 cout<<a[0]<<a[1]<<a[2]<<a[3];//這里不加空格的原因是,要求每行不能有多余空格 tmp=a[0];cnt++;//后移 }else{//輸出每行后續排列 cout<<" "<<a[0]<<a[1]<<a[2]<<a[3];cnt++;}}while(next_permutation(a,a+4));//cout<<endl;//cout<<"上一行的排列個數:"<<cnt-1<<endl; //cout<<"沒有前導零的所有的排列個數為:"<<num<<endl;}

總結

以上是生活随笔為你收集整理的C++STL之next_permutation使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。