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;
    
    }
  • 相关阅读:
    7种思维
    微服务架构
    最近面试被问到一个问题,AtomicInteger如何保证线程安全?
    Socket netty ...
    Spring-Boot配置文件web性能(服务器)配置项
    P2P互联网金融企业的四大转型方向
    分布式,微服务 区别联系 理解.
    几个好问题
    netty
    结构化思维
  • 原文地址:https://www.cnblogs.com/a719525932/p/7839685.html
Copyright © 2011-2022 走看看