给定数字能组成的最大时间
要求:
给定一个由 4 位数字组成的数组,返回可以设置的符合 24 小时制的最大时间。
最小的 24 小时制时间是 00:00,而最大的是 23:59。从 00:00 (午夜)开始算起,过得越久,时间越大。
以长度为 5 的字符串返回答案。如果不能确定有效时间,则返回空字符串。
c++实现:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
vector <int> good;
bool cmp(int a, int b)
{
return a > b;
}
class time
{
public:
string TIME(vector<int> &A)
{
sort(A.begin(), A.end(),cmp);
do
{
if (A[0] < 2 || (A[0] == 2 && A[1] < 4) && A[2] < 6)
{
return to_string(A[0]) + to_string(A[1]) + to_string(A[2]) + to_string(A[3]);
}
} while (prev_permutation(A.begin(), A.end()));
return "";
}
};
int main()
{
for (int i = 0; i < 4;i++)
{
int j;
cin >> j;
good.push_back(j);
}
time t;
t.TIME(good);
cout << t.TIME(good);
system("pause");
return 0;
}