출저 : 정올 (http://www.jungol.co.kr/prog/Hanal/hanalView.php?qs_code=1370)
#include <stdio.h>
#define DEBUG 1
typedef struct {
int requested_order;
int start_hour;
int end_hour;
}MEETING;
MEETING g_requested_meetings[501];
int g_num_requested_meetings;
MEETING g_scheduled_meeting[501];
int ScheduleMeetings();
void SortRequestedMeetings();
void PrintRequestedMeetings();
// input output
void SetInputFfromFile(char* fileName);
void PrintAnwserToFile(char* fileName, int i_last_meeting);
int main() {
SetInputFfromFile("input.txt");
PrintAnwserToFile("output.txt", ScheduleMeetings());
return 0;
}
void PrintRequestedMeetings() {
int im;
for (im = 1; im <= g_num_requested_meetings; im++){
printf("%d %d %d\n", g_requested_meetings[im].requested_order,
g_requested_meetings[im].start_hour,
g_requested_meetings[im].end_hour);
}
}
// sorting by end_hour
void SortRequestedMeetings() {
int i, j;
MEETING temp;
int end_hour_i;
int end_hour_j;
for (i = 1; i <= g_num_requested_meetings; i++) {
for (j = i + 1; j <= g_num_requested_meetings; j++) {
end_hour_i = g_requested_meetings[i].end_hour;
end_hour_j = g_requested_meetings[j].end_hour;
if (end_hour_i > end_hour_j) {
temp = g_requested_meetings[i];
g_requested_meetings[i] = g_requested_meetings[j];
g_requested_meetings[j] = temp;
}
}
}
}
int ScheduleMeetings() {
MEETING last_meeting, new_meeting;
int il, in; // index of last_meeting, index of new_meeting
SortRequestedMeetings();
if (DEBUG) PrintRequestedMeetings();
il = 1;
g_scheduled_meeting[il] = g_requested_meetings[il];
for (in = 2; in <= g_num_requested_meetings; in++) {
last_meeting = g_scheduled_meeting[il];
new_meeting = g_requested_meetings[in];
if (DEBUG) {
printf("%d : %d\n", last_meeting.end_hour, new_meeting.end_hour);
}
if (last_meeting.end_hour <= new_meeting.start_hour) {
g_scheduled_meeting[++il] = new_meeting;
}
}
return il;
}
void SetInputFfromFile(char* fileName) {
int i;
FILE *input = NULL;
input = fopen(fileName, "r");
if (input == NULL) {
fprintf(stderr, "file open error : %s\n", fileName);
return;
}
fscanf(input, "%d", &g_num_requested_meetings);
for (i = 1; i <= g_num_requested_meetings; i++) {
fscanf(input, "%d %d %d",
&(g_requested_meetings[i].requested_order),
&(g_requested_meetings[i].start_hour),
&(g_requested_meetings[i].end_hour));
}
fclose(input);
}
void PrintAnwserToFile(char* fileName, int i_last_meeting) {
int i;
FILE *output = NULL;
output = fopen("output.txt", "w");
if (output == NULL) {
fprintf(stderr, "file open error : %s\n", fileName);
return;
}
fprintf(output, "%d\n", i_last_meeting);
for (i = 1; i <= i_last_meeting; i++) {
fprintf(output, "%d ", (g_scheduled_meeting[i]).requested_order);
}
fprintf(output, "\n");
fclose(output);
}