Shanshan Pythoner Love CPP

hihoCoder 1408 The Lastest Time

2016-12-06

描述#1408 : The Lastest Time

Description

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.

Input and Output

输入

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

Codes

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool compare(int i, int j) {
    return i>j;
}

bool isValid(vector<int> vec) {
    if (vec[0] >2)  return false;
    if (vec[0] == 2 && vec[1]>3)    return false;
    if (vec[2] > 5) return false;
    return true;
}
int main() {
    vector<int> input(4);
    for (int i=0; i<4; i++)
        cin >> input[i];
    bool flag = false;
    sort(input.begin(), input.end(), compare);
    do {
        if (isValid(input)) {
            flag = true;
            cout << input[0] << input[1] << ":" << input[2] << input[3] << endl;
            break;
        }
        
    } while (prev_permutation(input.begin(), input.end()));
    if (!flag)  cout << "NOT POSSIBLE" << endl;
    return 0;
}

Comments

Content