1, 1, 2
1, 2, 1
2, 1, 1
1, 1, 2
1, 2, 1
2, 1, 1
榪欐槸璇ョ畻娉曠殑緙虹偣錛屼篃闄愬埗浜?jiǎn)瀹冪殑閫傜敤鑼冨洿銆?br />
紼嬪簭鎻忚堪濡備笅錛?/font>
#include聽(tīng)
<
iostream
>
#include聽(tīng)
<
algorithm
>
聽(tīng)
using
聽(tīng)
namespace
聽(tīng)std;聽(tīng)
//
聽(tīng)閫掑綊浜х敓R[k:n]鐨勬墍鏈夌殑鎺掑垪錛?strong>鍏冪礌鏄簰寮傜殑
template聽(tīng)
<
class
聽(tīng)Type
>
void
聽(tīng)permutation(Type聽(tīng)
*
R,
int
聽(tīng)k,
int
聽(tīng)n)
{
聽(tīng)聽(tīng)聽(tīng)聽(tīng)
if
(k
==
n)
聽(tīng)聽(tīng)聽(tīng)聽(tīng){
聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)
for
(
int
聽(tīng)i
=
0
;i
<
n;
++
i)
聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)cout聽(tīng)
<<
聽(tīng)R[i]聽(tīng)
<<
聽(tīng)
"
\t
"
;
聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)cout聽(tīng)
<<
聽(tīng)endl;
聽(tīng)聽(tīng)聽(tīng)聽(tīng)}
聽(tīng)聽(tīng)聽(tīng)聽(tīng)
else
聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)
for
(
int
聽(tīng)i
=
k;i
<
n;
++
i)
聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng){
聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)swap(R[k],R[i]);
聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)permutation(R,k
+
1
,n);
聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)swap(R[k],R[i]);
聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng)}
}
榪樻湁涓縐嶅緢綆鍗曠殑鏂規(guī)硶錛屼嬌鐢℅P涓殑鏂規(guī)硶
璇ョ畻娉曟槸STL涓殑鑼冨瀷綆楁硶錛屽綋鐒舵晥鏋滄槸寰堝ソ鐨勶紝涓嶄細(xì)鍑虹幇涓婇潰鐨勭畻娉曠殑鎯呭喌銆?br />
紼嬪簭鎻忚堪濡備笅錛?/font>
//
聽(tīng)浣跨敤娉涘瀷綆楁硶next_permutation()
#include聽(tīng)
<
iostream
>
#include聽(tīng)
<
vector
>
#include聽(tīng)
<
algorithm
>
聽(tīng)
using
聽(tīng)
namespace
聽(tīng)std;聽(tīng)
//
聽(tīng)浜х敓R[k:n]鐨勬墍鏈夌殑鎺掑垪
template聽(tīng)
<
class
聽(tīng)Type
>
聽(tīng)
void
聽(tīng)pernutation(Type聽(tīng)
*
R,
int
聽(tīng)k,
int
聽(tīng)n)
{
聽(tīng)vector
<
Type
>
聽(tīng)myVec;
聽(tīng)
int
聽(tīng)i,size聽(tīng)
=
聽(tīng)n聽(tīng)
-
聽(tīng)k;
聽(tīng)
for
(i聽(tīng)
=
聽(tīng)k;i聽(tīng)
<
聽(tīng)n;i
++
)
聽(tīng)聽(tīng)myVec.push_back(R[i]);
聽(tīng)
//
聽(tīng)浣跨敤next_permutation()鍑芥暟蹇呴』鏄湁搴忕殑鏁版嵁
聽(tīng)sort(myVec.begin(),myVec.end());
聽(tīng)聽(tīng)
聽(tīng)
do
聽(tīng){
聽(tīng)聽(tīng)
for
(i聽(tīng)
=
聽(tīng)
0
;i聽(tīng)
<
聽(tīng)size;i
++
)
聽(tīng)聽(tīng)聽(tīng)cout聽(tīng)
<<
聽(tīng)myVec[i]聽(tīng)
<<
聽(tīng)
"
\t
"
;
聽(tīng)聽(tīng)cout聽(tīng)
<<
聽(tīng)endl;
聽(tīng)}
聽(tīng)
while
(next_permutation(myVec.begin(),myVec.end()));
}
娉細(xì)榪欓噷鐨勫緟鍏ㄦ帓鐨勬暟鎹槸瀛樺湪鏁扮粍鎴栬呭悜閲忎腑鐨勩?/p>

]]>