U E D R , A S I H C RSS

Longest Nap/문보창

소감

2005/02/20 Accepted 0:00.002 64
그다지 구상이 필요없는 쉬운 문제였다. 그러나, 출력 오류 잡는데 상당한 시간을 허비했다. 01분과 1분은 분명 다르다.

코드

~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";
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:40
Processing time 0.0092 sec