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;
    
    }
  • 相关阅读:
    【翻译】谈 focus 和 blur 的事件代理
    【翻译】细分域名的优势
    Form窗体的Combobox键值对绑定
    日志
    ajaxload
    c#文件整理程序
    每天工作4小时的程序员
    每年这一天
    转载从交友到社交的个人成长
    旅行的意义
  • 原文地址:https://www.cnblogs.com/a719525932/p/7839685.html
Copyright © 2011-2022 走看看