Permutation

Description

 输入一个数字n,输出从1~n组成的数字的全排列,每个排列占一行,输出按照数值升序排列

比如输入2,则输出是:

12

21

又如输入3,则输出是:

123

132

213

231

312

321

Input

 第一行是一个整数m,代表有m个测试用例

接下来的m行,每行是一个整数n0 < n < 10

Output

 对于每个用例,输出它的全排列,每个排列占一行,输出按照数值降序排列

Sample Input
 Copy sample input to clipboard
2
2
3
Sample Output
12
21
123
132
213
231
312
321

Problem Source: 第四周 5班

先给出当时的做法:

#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <vector>
#include <iomanip>
#include <map>
#include <stack>
#include <functional>
#include <list>
using namespace std;

vector<vector<int> > ans;

void Perm(int k, int n, int a[]) {
    if (k == n) {
        vector<int> an;
        for (int i = 0; i < n; i++) an.push_back(a[i]);
        ans.push_back(an);
        return;
    }
    for (int i = k; i < n; i++) {
        swap(a[k], a[i]);
        Perm(k + 1, n, a);
        swap(a[i], a[k]);
    }
}

int main() {

    std::ios::sync_with_stdio(false);

    int i, n, a[20];

    int caseNum;

    cin >> caseNum;

    while (caseNum--) {

        cin >> n;

        if (cin.eof()) break;

        for (int i = 0; i < n; i++) a[i] = i + 1;

        ans.clear();

        Perm(0, n, a);

        sort(ans.begin(), ans.end());

        //cout << ans.size() << endl;

        for (int i = 0; i < ans.size(); i++) {
            for (int j = 0; j < ans[i].size(); j++) {
                cout << ans[i][j];
            }
            cout << endl;
        }

    }

    //getchar();
    //getchar();
    
    return 0;
}           

下面附上排列函数的合集:

#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <vector>
#include <iomanip>
#include <map>
#include <stack>
#include <functional>
#include <list>
using namespace std;

//函数说明
//next_permutation 头文件algorithm自带的得到下一个排列的函数
//fromPermutation 给出一个长度为n的排列,问他是第几个
//getPermutation得到n个数的第k个排列(不能有重复元素)
//makePermutation输出一个字符串的后续全排列, 输完或者输出到n个,支持重复元素和不重复元素,相当于不断调用next_permutation

inline int findMinBigger(string s, int p) {

    for (int i = s.size() - 1; i > p; i--) {
        if (s[i] > s[p]) return i;
    }

}

void makePermutation(string s, int n) {  // 输出一个字符串的后续全排列, 输完或者输出到n个,支持重复元素和不重复元素,相当于不断调用next_permutation

    int p = s.size() - 1;
    int q;
    int counter = 1;

    while (p != 0 && counter <= n) {

        q = p;
        p--;

        if (s[p] < s[q]) {

            int pMax = findMinBigger(s, p);
            swap(s[p], s[pMax]);
            reverse(s.begin() + q, s.end());

            cout << "Num " << setw(2) << counter << ": " << s << endl;
            counter++;

            p = s.size() - 1;
        }
    }

}

int fromPermutation(string s, int n) {  // 给出一个长度为n的排列,问他是第几个

    int * used = new int[n + 1];
    for (int i = 1; i <= n; i++) used[i] = false;

    int fac = 1;
    for (int i = 2; i < n; i++) fac *= i;

    int ans = 0;

    for (int i = 0; i < n - 1; i++) {

        int lessNum = 0;
        for (int j = 1; j <= n; j++) {
            if (!used[j] && j < s[i] - '0') lessNum++;
        }

        used[s[i] - '0'] = true;
        ans += lessNum * fac;
        fac /= n - i - 1;
    }

    return ans + 1;
}

string getPermutation(int n, int k) {  // 得到n个数的第k个排列(不能有重复元素)

    string ans;

    int * a = new int[n];
    for (int i = 0; i < n; i++) a[i] = i + 1;

    int fac = 1;
    for (int i = 2; i < n; i++) fac *= i;
    k--;

    bool * used = new bool[n + 1];
    for (int i = 1; i <= n; i++) used[i] = false;

    for (int i = n - 1; i >= 1; i--) {

        int num = k / fac;
        k %= fac;
        fac /= i;
        int lessNum = 0;
        int nextNum = 1;

        for (; nextNum <= n && lessNum < num; nextNum++) {
            if (!used[nextNum]) {
                lessNum++;
            }
        }
        while (used[nextNum]) nextNum++;

        used[nextNum] = true;
        ans.push_back(nextNum + '0');

    }

    for (int i = 1; i <= n; i++) {
        if (!used[i]) {
            ans.push_back(i + '0');
            break;
        }
    }

    return ans;
}



int main() {

    std::ios::sync_with_stdio(false);

    string s = "1234";

    cout << "The test for getPermutation, next_permutation, fromPermutation. ///" << endl;
    for (int i = 1; i <= 24; i++) {

        cout << "Num " << setw(2) << i << ": " 
             << "getPermutation-> " << getPermutation(4, i) << "; " 
             << "next_permutation-> " << s << "; "
             << "fromPermutation-> " << setw(2) << fromPermutation(s, 4) << "."
             << endl;
        
        next_permutation(s.begin(), s.end());

    }
    cout << "The test for getPermutation, next_permutation, fromPermutation. ///" << endl << endl;

    cout << "The test for next_permutation for duplicate data. /" << endl;
    s = "1233";
    for (int i = 1; i <= 12; i++) {
        cout << "Num " << setw(2) << i << ": " << s << endl;
        next_permutation(s.begin(), s.end());
    }
    cout << "The test for next_permutation for duplicate data. /" << endl << endl;

    cout << "The test for makePermutation for nonredundant data. ///" << endl;
    makePermutation("abcd", 100);
    cout << "The test for makePermutation for nonredundant data. ///" << endl << endl;

    cout << "The test for makePermutation for duplicate data. //" << endl;
    makePermutation("abcc", 100);
    cout << "The test for makePermutation for duplicate data. //" << endl << endl;

    

    getchar();
    getchar();
    
    return 0;
}                     
Permutation - Night - Time Ends.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值