-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActivitySelection.c
58 lines (45 loc) · 1.54 KB
/
ActivitySelection.c
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
#include<stdio.h>
#include<string.h>
struct Activity {
int start, finish;
char name;
};
int count = 0; int M = 0;
int activityCompare(struct Activity s1, struct Activity s2) {// Compare function for qsort()
return s1.finish - s2.finish;
}
void printMaxActivities(struct Activity activities[], int n) {
qsort(activities, n, sizeof(struct Activity), activityCompare); // sort the activities by finish time
int i = 0;
printf("\n\nSelected Activities: \n");
printf("\n%c ", activities[i].name);
count++;
for (int j = 1; j < n; j++) {
if (activities[j].start >= activities[i].finish) {
printf("\n%c ", activities[j].name);
count++;
i = j;
}
}
}
int main() {
printf("The charge for each activity: ");
scanf("%d", &M);
int n;
printf("Enter the no. of activity:");
scanf("%d", &n);
int finishCal = 0;
struct Activity activities[n];
printf("Enter the start time and duration:");
for (int i = 0; i < n; i++) {
scanf(" %c %d %d", &activities[i].name, &activities[i].start, &finishCal);
activities[i].finish = finishCal+activities[i].start;
}
for (int i = 0; i < n; i++) {
printf("\nName: %c, Start: %d, Finish: %d", activities[i].name, activities[i].start, activities[i].finish);
}
printMaxActivities(activities, n);
int profit = M*count; // Profit = the cost of each task X no. of task
printf("\n\nThe profit: %d", profit);
return 0;
}