每日一题之 hiho207 The Lastest Time

描述
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

思路:

由于只有四个数字,那么我们可以直接枚举这四个数的排列。然后将前两两位看作是hours和后两位数看做是minutes,从所有排列中选择一个符合要求并且最晚的时间输出就行

#include 
#include 
#include 
#include 
#include using namespace std;bool nextPermutation(string &str,int num) {int i;for ( i = num - 2; (i >= 0) && (str[i] >= str[i+1]); --i);if ( i < 0) return false;int k;for ( k = num - 1; (k > i) && (str[k] <= str[i]); --k);swap(str[i],str[k]);reverse(str.begin()+i+1,str.end());return true;
}int main()
{string str = "",x;for (int i = 0; i < 4; ++i) {cin >> x;str += x;}int hour = -1,minutes = -1;sort(str.begin(),str.end());do {int tmp_h = (str[0]-'0')*10 + str[1]-'0';int tmp_m = (str[2]-'0')*10 + str[3]-'0';if (tmp_h >= 0 && tmp_h <= 23 && tmp_m >= 0 && tmp_m <= 59) {if (tmp_h > hour || (tmp_h == hour && tmp_m > minutes)) {hour = tmp_h;minutes = tmp_m;}}}while(nextPermutation(str,4));if (hour == -1 || minutes == -1) cout << "NOT POSSIBLE" <else {if (hour < 10) cout << "0" << hour;elsecout << hour;cout << ":";if (minutes < 10) cout << "0" << minutes;elsecout << minutes;cout << endl;}return 0;
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部