-
Notifications
You must be signed in to change notification settings - Fork 0
/
Activity_Selection.cpp
64 lines (55 loc) · 1.18 KB
/
Activity_Selection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//Activity Selection Problem
#include <stdio.h>
struct activity {
int start;
int finish;
char name[2];
} a[6], temp, solution[6];
int main(void) {
// Input activities
for (int i = 0; i < 6; i++)
{
scanf("%d", &a[i].start);
scanf("%d", &a[i].finish);
//scanf("%s", a[i].name);
}
// Bubble sort
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 6 - i - 1; j++)
{
if (a[j].finish > a[j + 1].finish)
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
// Select activities
int count = 1;
solution[0] = a[0];
for (int i = 1; i < 6; i++)
{
if (a[i].start >= solution[count - 1].finish)
{
count++;
solution[count - 1] = a[i];
}
}
// Print selected activities
for (int i = 0; i < count; i++) {
printf("%d\t%d\t%s\n", solution[i].start, solution[i].finish, solution[i].name);
};
printf("Number of selected activities: %d\n", count);
return 0;
}
/*
Input Set
0 6
5 7
3 4
1 2
8 9
5 9
*/