-
Notifications
You must be signed in to change notification settings - Fork 0
/
milk.c
73 lines (65 loc) · 1.23 KB
/
milk.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
/*
ID: talos
LANG: C
PROG: milk
*/
#define INPUT "milk.in" /* input file*/
#define OUTPUT "milk.out" /* output file*/
#include <stdio.h>
#include <stdlib.h>
int main ()
{
/* definitions */
long int c1 , c2;
unsigned long int n, sum , temp ;
unsigned short int key , m;
FILE *in , *out;
/* read n , m , data */
in=fopen(INPUT, "r");
fscanf (in, "%lu %hu\n", &n , &m);
unsigned short int p[m];
unsigned long int q[m];
/* for each m read price and quantity*/
for (c1=0; c1 < m; c1++)
{
fscanf (in, "%hu %lu\n", &p[c1] , &q[c1]);
}
fclose (in);
/* sort by price , insertion sort */
for (c1 = 1; c1 < m; c1++)
{
key = p[c1];
temp = q[c1];
c2 = c1 - 1;
while ((c2>-1) && (p[c2] > key))
{
p[c2 + 1] = p[c2];
q[c2 + 1] = q[c2];
c2--;
}
p[c2 + 1 ] = key;
q[c2 + 1 ] = temp;
}
sum=0;
c1=0;
/* while total quantity < n*/
while (n > 0)
{
/* get milk (quantity) and compute sum=p*q */
if (n > q[c1])
{
sum+=p[c1] * q[c1];
n-=q[c1];
} else
{
sum+=n * p[c1];
break;
}
c1++;
}
/* write sum to file and exit */
out=fopen(OUTPUT, "w");
fprintf(out, "%lu\n", sum);
fclose(out);
return 0;
}