題目描述: 給定一個數組,裏面有6個整數,求這個數組可以表示的最大24進制時間是多少,輸出這個時間,沒法表示輸出invalid。ios
輸入描述: 輸入爲一個整數數組,數組內有六個整數。 輸入整數數組長度爲6,不須要考慮它長度,元素爲0或者正整數,6個數字每一個數字只能使用一次;數組
輸出描述: 輸出爲一個24進制格式的時間,或者字符串「invalid」。spa
示例1 輸入 [0,2,3,0,5,6] 輸出 23:56:00.net
示例1 輸入 [9,9,9,9,9,9] 輸出 invalid <br><br>code
1.思考blog
2.實現ci
<br>字符串
#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; vector<int> CompTime(vector<int>& num){ vector<int> time; //Invalid if (num[3] > 5 || num[1] > 3 || num[0] > 2){ time.clear(); return time; } //Hour bool hflag = false; int len = num.size(); int a, b; for (int i = len - 1; i >= 0; i--){ if (num[i] < 2){ a = i; time.push_back(num[i]); for (int j = len - 1; j >= 0; j--){ if (num[j] <= 9 && j != i){ b = j; hflag = true; time.push_back(num[j]); break; } } if (hflag){ num.erase(num.begin() + max(a, b)); num.erase(num.begin() + min(a, b)); break; } } else if (num[i] == 2){ a = i; time.push_back(num[i]); for (int j = len - 1; j >= 0; j--){ if (num[j] < 4 && j != i){ b = j; hflag = true; time.push_back(num[j]); break; } } if (hflag){ num.erase(num.begin() + max(a, b)); num.erase(num.begin() + min(a, b)); break; } } } if (hflag == false){ time.clear(); return time; } //Minute len = num.size(); bool mflag = false; for (int i = len - 1; i >= 0; i--){ if (num[i] <= 5){ a = i; time.push_back(num[i]); for (int j = len - 1; j >= 0; j--){ if (num[j] <= 9){ b = j; mflag = true; time.push_back(num[j]); break; } } if (mflag){ num.erase(num.begin() + max(a, b)); num.erase(num.begin() + min(a, b)); break; } } } if (mflag == false){ time.clear(); return time; } //Second bool sflag = false; if (num[1] <= 5){ sflag = true; time.push_back(num[1]); time.push_back(num[0]); } else if (num[0] <= 5){ sflag = true; time.push_back(num[0]); time.push_back(num[1]); } if (sflag == false){ time.clear(); } return time; } int main(){ string input; while (getline(cin, input)){ vector<int> num, time; for (int i = 1; i < 12; i=i+2){ num.push_back(input[i] - '0'); } sort(num.begin(), num.end()); time = CompTime(num); if (time.empty()){ cout << "invalid" << endl; } else{ cout << time[0] << time[1] << ":" << time[2] << time[3] << ":" << time[4] << time[5] << endl; } } return 0; }