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”.
代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

#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 (){
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%dn" , a[0] , a[1] , a[2] , a[3]) ;
}else{
printf("NOT POSSIBLEn") ;
}
return 0 ;
}