include stdio define SIZE 40 int main int argc const char argv int arr

 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
#include <stdio.h>
#define SIZE 40
int main(int argc, const char * argv[])
{
int arr[16][4] = {
{0,0,0,0}, {0,0,0,1}, {0,0,1,0}, {0,0,1,1},
{0,1,0,0}, {0,1,0,1}, {0,1,1,0}, {0,1,1,1},
{1,0,0,0}, {1,0,0,1}, {1,0,1,0}, {1,0,1,1},
{1,1,0,0}, {1,1,0,1}, {1,1,1,0}, {1,1,1,1}
};
unsigned short chosen[16];
unsigned int result[16];
int wts[] = {29, 10, 20, 15};
int calories[] = {34, 20, 31, 24};
for (int i = 0; i < 16; i++) {
int sum = 0;
for (int j = 0; j < 4; j++) {
if (arr[i][j] == 1)
sum += wts[j];
}
if (sum > SIZE || sum == 0)
chosen[i] = 0;
else
chosen[i] = 1;
}
for (int i = 0; i < 16; i++) {
int sum = 0;
if (chosen[i] == 1) {
for (int j = 0; j < 4; j++) {
if (arr[i][j] == 1)
sum += calories[j];
}
result[i] = sum;
}
else {
result[i] = 0;
}
}
int max = result[0];
for (int i = 0; i < 16; i++) {
if (max < result[i])
max = result[i];
}
printf("Result: %d\n", max);
return 0;
}