黑科技(next_permutation和prev_permutation)
生活随笔
收集整理的這篇文章主要介紹了
黑科技(next_permutation和prev_permutation)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
關(guān)于數(shù)列的全排列問(wèn)題:
如果我們要得到a[]={2,1,3}的全排列,我們可以有如下兩種做法:
1、將數(shù)列排成升序:(1,2,3),如下寫代碼:
do{cout << a[0] << " " << a[1] << " " << a[2] << endl; } while (next_permutation(a,a+3))
2、將數(shù)列排成降序:(3,2,1),如下寫代碼 : do{cout << a[0] << " " << a[1] << " " << a[2] << endl; } while (prev_permutation(a,a+3))即可得到數(shù)列的全排列。
如果對(duì)vector<int> a進(jìn)行全排列,則參數(shù)可以寫成next_permutation(a.begin(),a.end())。
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的黑科技(next_permutation和prev_permutation)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: poj2104(区间第k大+离散化)
- 下一篇: 判断直线与线段是否相交,相交则输出交点x