HihoCoder 1408 The Lastest Time

1408 : The Lastest Time

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

描述

What is latest time you can make with 4 digits A, B, C and D?

For example if the 4 digits are 1, 0, 0, 0, you can make 4 times with them: 00:01, 00:10, 01:00, 10:00. The lastest time will be 10:00. Note a valid time is between 00:00 and 23:59.

输入

One line with 4 digits A, B, C and D, separated by a space. (0 <= A, B, C, D <= 9)

输出

Output the lastest time in the format "hh:mm". If there is no valid time output NOT POSSIBLE.  

样例输入

0 9 0 0

样例输出

09:00

题解

题目大意是给四个数,输出这四个数能组成的最晚的时间。枚举就好,先倒序排序,再用 “反向全排列( prev_permutation() )” 判断是否含有符合条件的 (时间在 00:00 和 23:59 之间寻找最大的),如果没有符合条件的输出 “NOT POSSIBLE”.
代码如下:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <utility>
#define ll long long
#define ull_ unsigned long long

using namespace std ;

int main(){
    int a[5] ;
    for ( int i = 0 ; i < 4 ; i ++ ){
        cin >> a[i] ;
    }
    sort(a , a + 4 , greater<int>()) ;
    bool check = false ;
    do{
        if (a[0] * 10 + a[1] <= 23 && a[2] * 10 + a[3] <= 59 ){
            check = true ;
            break ;
        }
    }while(prev_permutation(a , a + 4)) ;
    if ( check ){
        printf("%d%d:%d%d\n" , a[0] , a[1] , a[2] , a[3]) ;
    }else{
        printf("NOT POSSIBLE\n") ;
    }
    return 0 ;
}

转载于:https://www.cnblogs.com/Cantredo/p/9867373.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值