ZCMU 1620: 全排列

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 242  Solved: 112

Description

給定n個數 a[0] , a[1] ........ a[n-1], 輸出其全排列。

 

Input

第一行輸入一個數n,(n<7)

接下來一行輸入n個數。

 

Output

按字典序從小到大輸出全排列

 

Sample Input

3 1 2 3 3 1 2 2

Sample Output

1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 1 2 2 2 1 2 2 2 1

HINT

 

Source

題解:記錄一下全排列的格式

代碼:

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[10];
int main()
{
    int n;
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
            cin>>a[i];
        sort(a,a+n);
        do
        {
            cout<<a[0];
            for(int i=1;i<n;i++)
                cout<<" "<<a[i];
            cout<<endl;
        }while(next_permutation(a,a+n));
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章