九个数的全排列(避免重复出现)

题意: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\n", s);///生成所有比当前的字符串大的字符串
    while (std::next_permutation(s, s + lenth))
        printf("%s\n",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]!='\0'; 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("\n");
    while(next_permutation(a,a+l))
    {
        for(int i=0; i<l; i++)
            printf("%d",a[i]);
        printf("\n");
    }
    return 0;

}

 

转载于:https://www.cnblogs.com/a719525932/p/7839685.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值