zoukankan      html  css  js  c++  java
  • hdu1716(库函数next_permutation)

    题目意思:

    现有四张卡片,用这四张卡片能排列出非常多不同的4位数,要求按从小到大的顺序输出这些4位数。

    注意首位没有前导0

    http://acm.hdu.edu.cn/showproblem.php?

    pid=1716


    题目分析:

    库函数next_permutation()应用,直接调用库函数,输出时注意前导0,和空格。祥见代码


    AC代码:


    #include<iostream>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    int main()
    {
        int a[4],ok=0;
        cin>>a[0]>>a[1]>>a[2]>>a[3];
        while(1){
            if(a[0]+a[1]+a[2]+a[3]==0) break;
            sort(a,a+4);//排序
            int k=a[0];
            if(a[0]!=0) cout<<a[0]<<a[1]<<a[2]<<a[3];
            while(next_permutation(a,a+4)){
                if(a[0]==k&&a[0]!=0) cout<<" "<<a[0]<<a[1]<<a[2]<<a[3];
                else{
                    if(a[0]!=0){
                        if(k!=0) cout<<endl;//换行
                        cout<<a[0]<<a[1]<<a[2]<<a[3];
                    }
                    k=a[0];
                }
            }
            cout<<endl;
            cin>>a[0]>>a[1]>>a[2]>>a[3];//仅仅有下次不退出才换行
            if(a[0]+a[1]+a[2]+a[3]!=0) cout<<endl;
        }
        return 0;
    }




  • 相关阅读:
    Python抽象及异常处理
    Python函数练习
    Python字典练习
    Python字符串练习
    Python列表、元组练习
    树莓派搭建网站
    嵌入式特点、组成
    创建队列 出队 入队 显示队列(链式)
    面试题--1 输入时间要求输出下一秒
    图像傅里叶变换的意义
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/7100548.html
Copyright © 2011-2022 走看看