~cpp // no10191 - Longest Nap #include <iostream> #include <algorithm> using namespace std; struct Promise { int start; int end; bool operator() (const Promise & a, const Promise & b) { if (a.start < b.start) return true; else return false; } }; const int MAX = 100; const int START_TIME = 10; const int END_TIME = 18; inline void eatline() { while (cin.get() != '\n' && cin.peek() != EOF) continue; }; int make_schedule(Promise * pro); Promise set_naptime(const Promise * pro, const int & nPromise); void show(const Promise & nap, const int & index); int main() { Promise promise[MAX]; int i = 0; while (cin.peek() != EOF) { i++; int nPromise = make_schedule(promise); sort(&promise[0], &promise[nPromise], Promise()); Promise nap = set_naptime(promise, nPromise); show(nap, i); } return 0; } int make_schedule(Promise * pro) { int nPromise; cin >> nPromise; int i; int shour, smin, ehour, emin; for (i=0; i<nPromise; i++) { cin >> shour, cin.get(); cin >> smin, cin.get(); cin >> ehour, cin.get(); cin >> emin; eatline(); pro[i].start = shour * 60 + smin; pro[i].end = ehour * 60 + emin; } return nPromise; } Promise set_naptime(const Promise * pro, const int & nPro) { Promise nap; int naptime = pro[0].start - START_TIME * 60; nap.start = START_TIME * 60; int i; for (i=1; i<nPro; i++) { if (naptime < (pro[i].start - pro[i-1].end)) { naptime = pro[i].start - pro[i-1].end; nap.start = pro[i-1].end; } } if (naptime < (END_TIME * 60 - pro[nPro-1].end)) { naptime = END_TIME * 60 - pro[nPro-1].end; nap.start = pro[nPro-1].end; } nap.end = nap.start + naptime; return nap; } void show(const Promise & nap, const int & index) { cout << "Day #" << index << ": the longest nap starts at " << nap.start/60 << ":"; if (nap.start%60 < 10) cout << "0" << nap.start%60; else cout << nap.start%60; cout << " and will last for "; int during = nap.end - nap.start; if (during >= 60) { cout << during/60 << " hours and " << during%60 << " minutes.\n"; } else cout << during << " minutes.\n"; }