04/06/24 07:28
941 :Socket774 :04/04/18 22:00 ID:++V30W1q
はい、Cで完全に動くヤツな
forは使っていいんだろ?
#include <stdio.h>
int main()
{
struct item {
char classification[4];
int money;
} items[] = {
{"100", 1000},
{"010", 2000},
{"001", 3000},
{"100", 100},
{"010", 55},
{"001", 15}
};
int amounts[8] = {0, 0, 0, 0, 0, 0, 0, 0};
int i;
for (i = 0; i < sizeof items / sizeof *items; i++) {
amounts[(items[i].classification[0] - '0') * 4 +
(items[i].classification[1] - '0') * 2 +
(items[i].classification[2] - '0')] += items[i].money;
}
for (i = 0; i < sizeof amounts / sizeof *amounts; i++) {
printf("amounts %d%d%d : %d\n", i / 4, (i % 4) / 2, i % 2, amounts[i]);
}
return 0;
}