zoukankan      html  css  js  c++  java
  • STL的排列函数及字符串的排序方法

    next_permutation(a,a+n);   a代表数组的头地址,a+n代表数组的长度。

    运用该函数,a数组将变成原排列的下一个排列。

    与之相反的函数为prev_permutation(a,a+n);

    #include <bits/stdc++.h>
    
    using namespace std;
    
    typedef long long LL;
    
    int f(int n) {
        int sum = 1;
        for(int i = 2; i <= n; i++)
            sum *= i;
        return sum;
    }
    int main() {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        int a[10];
        int n;
        scanf("%d",&n);
        for(int i = 1; i <= n; i++) {
            a[i] = i;
        }
        for(int i = 1; i <= f(n); i++)    {
            for(int j = 1; j <= n; j++)
                cout << a[j];
            next_permutation(a+1,a+n+1);    //prev_permutation(a+1,a+n+1);
            cout << endl;
        }
        return 0;
    }

    上面是int型,下面的char型和string型的写法

    第一行为输入,后面几行为输出,上题的整型输出类似

    #include <bits/stdc++.h>
    
    using namespace std;
    
    typedef long long LL;
    
    int f(int n) {
        int sum = 1;
        for(int i = 2; i <= n; i++)
            sum *= i;
        return sum;
    }
    int main() {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        string b; //char a[100];
        cin >> b; //scanf("%s",a);
        sort(b.begin(), b.end()); //sort(a, a+stlen(a));
        for(int j = 1; j <= f(b.length()); j++) {// f(strlen(a))
            cout << b << endl; // printf("%s
    ",a);
        next_permutation(b.begin(),b.end()); // next_permutation(a, a+strlen(a));    
        }
        return 0;
    }
  • 相关阅读:
    socket注意
    PCM音频文件编码
    题外:分类篇(音乐风格分类)基于BP神经网络
    MFCC特征提取过程详解
    语音信号分析
    k-means聚类
    c++关键字详解
    vs中项目解决方案和项目的关系
    条件编译#ifdef 和#endif
    c++快捷键
  • 原文地址:https://www.cnblogs.com/creativepower/p/6798049.html
Copyright © 2011-2022 走看看