-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmooncakeSales.c
106 lines (95 loc) · 1.8 KB
/
mooncakeSales.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
mcake_t:
inv: inventory.
tp: total price.
up: unit price.
*/
typedef struct
{
double up;
double inv;
double tp;
} mcake_t;
void mcake_calc_up(mcake_t *mcake)
{
//if (mcake->inv != 0 && mcake->tp != 0)
if (1) // Disabled after debug.
{
mcake->up = mcake->tp / mcake->inv;
}
else
{
perror("Empty Mcake");
exit(EXIT_FAILURE);
}
}
// Compare function for mcake_t in qsort().
int mcake_compare(const void *a, const void *b)
{
//if (((mcake_t *)a)->up == 0 || ((mcake_t *)b)->up == 0)
if (0) // Disabled after debug.
{
perror("Invalid unit price");
exit(EXIT_FAILURE);
}
// Compare.
if (((mcake_t *)a)->up > ((mcake_t *)b)->up)
{
return -1;
}
else if (((mcake_t *)a)->up < ((mcake_t *)b)->up)
{
return 1;
}
else
{
return 0;
}
}
double max_income(mcake_t mcakes[], int n_cat, double demand)
{
// Sort mcakes.
size_t mcake_size = sizeof(mcake_t);
// Sort in acsending order.
qsort(mcakes, n_cat, mcake_size, mcake_compare);
// Calc income.
double income = 0;
for (int i = 0; i < n_cat; i++)
{
if (mcakes[i].inv < demand)
{
demand -= mcakes[i].inv;
income += mcakes[i].inv * mcakes[i].up;
}
else
{
income += demand * mcakes[i].up;
break;
}
}
return income;
}
int main(void)
{
int n_cat;
double demand;
scanf("%d%lf", &n_cat, &demand);
mcake_t mcakes[n_cat];
memset(mcakes, 0, sizeof(mcake_t) * n_cat);
// Input mooncake stats.
for (int i = 0; i < n_cat; i++)
{
scanf("%lf", &mcakes[i].inv);
}
for (int i = 0; i < n_cat; i++)
{
scanf("%lf", &mcakes[i].tp);
mcake_calc_up(&mcakes[i]);
}
// Input mooncake stats finish.
printf("%.2lf", max_income(mcakes, n_cat, demand));
return 0;
}