zoukankan      html  css  js  c++  java
  • 51nod 1384 全排列 【递归】

    基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
     收藏
     关注
    给出一个字符串S(可能有重复的字符),按照字典序从小到大,输出S包括的字符组成的所有排列。例如:S = "1312",
    输出为:
     
    1123
    1132
    1213
    1231
    1312
    1321
    2113
    2131
    2311
    3112
    3121
    3211
    Input
    输入一个字符串S(S的长度 <= 9,且只包括0 - 9的阿拉伯数字)
    Output
    输出S所包含的字符组成的所有排列
    Input示例
    1312
    Output示例
    1123
    1132
    1213
    1231
    1312
    1321
    2113
    2131
    2311
    3112
    3121
    3211
    #include<stdio.h>
    #include<algorithm>
    using namespace std;
    char s[14];
    int a[14],b[14],n;
    
    void A(int *b,int ans)
    {
        int i,j,ans1,ans2;
        if(ans == n+1)
        {
            for(i = 1; i <= n; i ++)
                printf("%d",b[i]);
            printf("
    ");
        }
        else
        {
            for(i = 1; i <= n; i ++)
            {
                if(a[i]!=a[i-1])
                {
                    ans1 = ans2 = 0;
                    for(j = 1;j < ans; j ++)if(b[j]==a[i])ans1++;
                    for(j = 1; j <= n; j ++)if(a[i]==a[j])ans2++;
                    if(ans1 < ans2)
                    {
                        b[ans] = a[i];
                        A(b,ans+1);
                    }
                }
                
            }
        }
        return;
    }
    
    int main()
    {
        int i;
        while(scanf("%s",s)!=EOF)
        {
            a[0] = -1;
            for(i = 0; s[i]!=''; i ++)
                a[i+1] = s[i]-'0';
            n = i;
            sort(a+1,a+n+1);
            A(b,1);
        }
        return 0;
    }
  • 相关阅读:
    1.2顺序表
    1.1数据结构
    Java 造假数据
    Python造假数据,用这个库
    真香 用这七大Python效率工具
    mybatis 详情
    MySQL 的 INSERT ··· ON DUPLICATE KEY UPDATE
    mysql之case when then 经典用法
    SELECT NOW(),CURDATE(),CURTIME()
    MySQL CONCAT_WS 函数
  • 原文地址:https://www.cnblogs.com/hellocheng/p/7444306.html
Copyright © 2011-2022 走看看