zoukankan      html  css  js  c++  java
  • 九个数的全排列(避免重复出现)

    题意:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1384

    使用标准库里面的next_permutation()函数,这个函数是生成所有比当前的字符串大的字符串,所以最开始经过sort()排序之后,要先打印出当前的字符串。
    C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序、std::prev_permutation提供降序。

    两个代码本质是一样的  一个从字符串出现,一个从数组

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<queue>
    #include<map>
    #include<vector>
    #include<math.h>
    #include<string>
    #include<numeric>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define LL long long
    #define N 106
    #define Lson rood<<1
    #define Rson rood<<1|1
    int main()
    {
        char s[21];
        scanf("%s", s);
        int lenth = strlen(s);
        std::sort(s, s + lenth);///将字符串按照字典数排序
        printf("%s
    ", s);///生成所有比当前的字符串大的字符串
        while (std::next_permutation(s, s + lenth))
            printf("%s
    ",s);
        return 0;
    }

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    int main()
    {
        char s[10];
        int a[10];
        scanf("%s",s);
        for(int i=0; s[i]!=''; i++)
            a[i]=s[i]-'0';
        int l=strlen(s);
        sort(a,a+l);
        for(int i=0; i<l; i++)
                printf("%d",a[i]);
            printf("
    ");
        while(next_permutation(a,a+l))
        {
            for(int i=0; i<l; i++)
                printf("%d",a[i]);
            printf("
    ");
        }
        return 0;
    
    }
  • 相关阅读:
    【css】怎么让Chrome支持小于12px 的文字
    java操作linux,调用shell命令
    20个非常有用的Java程序片段
    Java集合详解
    SVN使用指南
    利用SQL语句查询数据库中所有表
    HttpClient-03Http状态管理
    HttpClient-02连接管理
    HttpClient-01基本概念
    IDEA安装插件
  • 原文地址:https://www.cnblogs.com/a719525932/p/7839685.html
Copyright © 2011-2022 走看看