zoukankan      html  css  js  c++  java
  • 排列2

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    Description

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

    Input

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

    Output

    对每组卡片按从小到大的顺序输出所有能由这四张卡片组成的4位数,千位数字相同的在同一行,同一行中每个四位数间用空格分隔。 
    每组输出数据间空一行,最后一组数据后面没有空行。 
    http://www.cnblogs.com/heyonggang/archive/2013/08/07/3243477.html
     

    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
     
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    
    using namespace std;
    
    #define N 100000
    
    int a[4], num[N], ans;
    
    void dfs(int k)  // 全排列
    {
        if(k == 4)
        {
            if(a[0])
                num[ans++] = a[0]*1000 + a[1]*100 + a[2]*10 + a[3]; // 第一位非0,1个4位数
            return ;
        }
        else
        {
            for(int i = 0; i < 4; i++)
            {
                swap(a[i], a[k]);
                dfs(k+1);
                swap(a[k], a[i]); 
            }
        }
    }
    int main()
    {
        int flag = 0;
    
        while(cin >> a[0] >> a[1] >> a[2] >> a[3], a[0] + a[1] + a[2] + a[3])
        {
            memset(num, 0, sizeof(num));
            ans = 0;
    
            dfs(0);
    
            sort(num, num+ans); // unique之前一般都sort一下
            ans = unique(num,num+ans) - num;  // 去重函数
    
            num[ans] = 0; // 最后一个置为0,
    
            if(flag)
                cout << endl;
    
            for(int i = 0; i < ans; i++)
            {
                cout << num[i];
                if(num[i]/1000 == num[i+1]/1000)
                    cout << ' ';  // 第一位数相同输空格,不相同换行
                else
                    cout << endl;
            }
            flag = 1;
        }
    
        return 0;
    }
    让未来到来 让过去过去
  • 相关阅读:
    Introduction to Oracle9i: SQL left join 和 left outer join 的区别
    ORACLE10G RMAN 命令
    Oracle管理与维护.手工创建数据库以及脚本
    RMAN 备份基本用法
    ASM 常用概念解释
    oracle学习笔记之二:数据类型之DATETIME 收藏
    10g中表监控与statistics_level
    Oracle 学习笔记: RMAN常用命令
    Oracle 进程类别
    ORACLE TRUNC()函数
  • 原文地址:https://www.cnblogs.com/Tinamei/p/4658146.html
Copyright © 2011-2022 走看看