zoukankan      html  css  js  c++  java
  • hdu1716排列2(stl:next_permutation+优先队列)

    排列2

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 5437    Accepted Submission(s): 2072


    Problem Description
    Ray又对数字的列产生了兴趣:
    现有四张卡片,用这四张卡片能排列出非常多不同的4位数,要求按从小到大的顺序输出这些4位数。
     

    Input
    每组数据占一行,代表四张卡片上的数字(0<=数字<=9),假设四张卡片都是0,则输入结束。


     

    Output
    对每组卡片按从小到大的顺序输出全部能由这四张卡片组成的4位数,千位数字同样的在同一行,同一行中每一个四位数间用空格分隔。
    每组输出数据间空一行。最后一组数据后面没有空行。
     

    Sample Input
    1 2 3 4 1 1 2 3 0 1 2 3 0 0 0 0
     

    Sample Output
    1234 1243 1324 1342 1423 1432 2134 2143 2314 2341 2413 2431 3124 3142 3214 3241 3412 3421 4123 4132 4213 4231 4312 4321 1123 1132 1213 1231 1312 1321 2113 2131 2311 3112 3121 3211 1023 1032 1203 1230 1302 1320 2013 2031 2103 2130 2301 2310 3012 3021 3102 3120 3201 3210
     

    Source

    2007省赛集训队练习赛(2)

    还有比这道题格式更牛的吗?我都吐血了。

    。。

    PE了7 8 次。。最后试一试的态度 居然AC了。。

    放一张输出的图片吧

    感觉看了输出就懂的了这道题的格式。

    全排+优先队列:

    #include <stdio.h>
    #include <queue>
    #include <algorithm>
    using namespace std;
    int main()
    {
        int a[4];
        priority_queue<int,vector<int>,greater<int> >s;//优先队列。greater换成less就是先出大的
        for(int i=0;i<4;i++)
        scanf("%d",&a[i]);
        while(1)
        {
            if(a[0]==a[1]&&a[1]==a[2]&&a[2]==a[3]&&a[0]==0)
            break;
            do
            {
                int sum=0;
                for(int i=3;i>=0;i--)
                sum=sum*10+a[i];
                if(sum>1000)
                s.push(sum);
            }while(next_permutation(a,a+4));//全排公式
            for(int i=1;i<=9;i++)
            {
                if(s.top()/1000==i)
                {
                    printf("%d",s.top()),s.pop();
                    while(s.top()/1000==i&&!s.empty())
                    {
                        printf(" %d",s.top());
                        s.pop();
                    }
                    printf("
    ");
                }
            }
            for(int i=0;i<4;i++)//
            scanf("%d",&a[i]);//
            if(a[0]==a[1]&&a[1]==a[2]&&a[2]==a[3]&&a[0]==0)//
            break;//
            else//
            printf("
    ");//这么多 我不过为了格式。

    。这道题 多么恶心。。 } return 0; }

    四个for循环做的:

    #include<stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    int main()
    {
        int a[4],q,stamp[30];
        scanf("%d %d %d %d",&a[0],&a[1],&a[2],&a[3]);
        while(1)
        {
            if(a[0]==a[1]&&a[1]==a[2]&&a[2]==a[3]&&a[1]==0)
            break;
            memset(stamp,0,sizeof(stamp));
            q=0;
            for(int i=0;i<4;i++)
            for(int j=0;j<4;j++)
            for(int k=0;k<4;k++)
            for(int t=0;t<4;t++)
            {
                if(i!=j&&i!=k&&i!=t&&j!=k&&j!=t&&k!=t&&a[i]>0)
                stamp[q++]=a[i]*1000+a[j]*100+a[k]*10+a[t];
            }
            sort(stamp,stamp+q);
            for(int i=1;i<=9;i++)
            {
                for(int j=0;j<q;j++)
                {
                    if(stamp[j]!=stamp[j+1]&&stamp[j]/1000==i)
                    {
                        printf("%d",stamp[j]),j++;
                        while(stamp[j]/1000==i)
                        {
                            while(stamp[j]==stamp[j+1])
                            j++;
                            printf(" %d",stamp[j]);
                            j++;
                        }
                        printf("
    ");
                    }
                }
            }
            scanf("%d %d %d %d",&a[0],&a[1],&a[2],&a[3]);
            if(a[0]==a[1]&&a[1]==a[2]&&a[2]==a[3]&&a[1]==0)
            break;
            else
            printf("
    ");
        }
        return 0;
    }

    还有个搜索能够做。请小伙伴睡觉们自己做吧~


  • 相关阅读:
    Windows Phone 独立存储(IsolatedStorageFile) 简单
    Windows Phone HttpWebRequest 简单
    PHP register_shutdown_function函数详解 简单
    sap 系统参数设置
    记帐码: (posting key)
    FICO 面试题
    RFUMSV00 营业税预先申报
    零售业附加商品的应用
    FI 财务会计事务码
    ABAP MODIF ID 作用
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5405618.html
Copyright © 2011-2022 走看看