HDU 1716 排列2

原题代号:HDU 1716

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1716

原题描述:

排列2

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

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
 
还是坑在格式上,格式错误两次。。。水题,暴力四个循环再加排除出现过的数值即可

AC代码:

# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <iostream>
# include <fstream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <math.h>
# include <algorithm>
using namespace std;
# define pi acos(-1.0)
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define For(i,n,a) for(int i=n; i>=a; --i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define Fo(i,n,a) for(int i=n; i>a ;--i)
typedef long long LL;
typedef unsigned long long ULL;

int num[20],ans;

bool serch(int no)
{
    for(int i=0;i<ans;i++)
        if(no==num[i])return true;
    return false;
}

int main()
{
    int a[4],t=0;
    while(scanf("%d%d%d%d",&a[0],&a[1],&a[2],&a[3]),a[0]||a[1]||a[2]||a[3])
    {
        ans=0;
        if(t)cout<<endl;
        t=1;
        sort(a,a+4);
        for(int i=0;i<4;i++)
        {
            int flag=0;
            if(a[i]==0)continue;
            for(int j=0;j<4;j++)
            {
                if(j==i)continue;
                for(int k=0;k<4;k++)
                {
                    if(k==i||k==j)continue;
                    for(int t=0;t<4;t++)
                    {
                        if(t==i||t==j||t==k)continue;
                        if(serch(a[i]*1000+a[j]*100+a[k]*10+a[t])==0)
                        {
                            num[ans]=a[i]*1000+a[j]*100+a[k]*10+a[t];
                            ans++;
                            printf(flag==0?"%d%d%d%d":" %d%d%d%d",a[i],a[j],a[k],a[t]);
                            flag=1;
                        }
                    }
                }
            }
            if(flag)cout<<endl;
        }
    }
    return 0;
}

 

  

posted @ 2017-07-17 20:45  韵祈  阅读(166)  评论(0编辑  收藏  举报