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

    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
    #include <set>
    #include
    <cstring>
    #include
    <iostream>
    using namespace std;
    set<int> s;
    bool flag[5];
    int n[5];


    void dfs(int a,int num)
    {
    int b;
    if(num==4){
    s.insert(a);
    return;
    }
    else
    {
    for(b=1;b<=4;b++){
    if(!flag[b])
    {flag[b]
    =true;
    dfs(a
    *10+n[b],num+1);
    flag[b]
    =false;
    }
    }
    }
    return;
    }
    int main(){
    int k=0;
    int d,e,f;
    while(cin>>n[1]>>n[2]>>n[3]>>n[4]){
    set<int>::iterator t;
    if(!n[1]&&!n[2]&&!n[3]&&!n[4]) //判断是否全是0
    break;
    s.clear();
    if(k) //每组输出数据间空一行
    cout<<endl;
    k
    ++;
    memset(flag,
    false,sizeof(flag));
    for(d=1;d<=4;d++){
    flag[d]
    =true;
    dfs(n[d],
    1);
    flag[d]
    =false;
    }
    for(t=s.begin();t!=s.end();t++){
    if(*t>=1000)break;
    }
    e
    =*t/1000;//找出千位的值
    cout<<*t;//输出千位相同的第一个数字
    t++;
    for(;t!=s.end();t++){
    if(*t/1000!=e){
    cout
    <<endl;
    cout
    <<*t;
    e
    =*t/1000;
    }
    else {
    cout
    <<" "<<*t;//输出千位相同剩余的数字
    }
    }
    cout
    <<endl;
    }
    return 0;
    }
  • 相关阅读:
    2007上半年网络游戏企业报告总结
    2007中国网络游戏调查报告
    3G门户网总裁张向东:怎分享3G大蛋糕
    网络在线游戏开发心得(服务器端、Java)
    Apache服务器SSL配置
    详解 Windows下apache 实现 SSL
    10个既绚又实用的JQuery特效教程
    Zend Studio 快捷键
    jQuery最佳实践
    2月份13个jQuery最佳插件推荐
  • 原文地址:https://www.cnblogs.com/aboutblank/p/2086695.html
Copyright © 2011-2022 走看看