計算 HEIHEI 某天的總心情值。規則:
t (1 ≤ t ≤ 100)輸入: 7 Sunday 21 11 71 Friday 27 44 Tuesday 29 34 Monday 13 18 Tuesday 73 5 Friday 35 85 Saturday 68 25 11 輸出: 103 71 63 31 78 120 104
week[7] 對應索引 0~6#include <stdio.h>
#include <string.h>
int main() {
int t;
char week[7][20] = {"Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"};
scanf("%d", &t);
while (t--) {
char day[20];
int a, b, c;
scanf("%s", day);
for (int i = 0; i < 7; i++) {
if (strcmp(day, week[i]) == 0) {
if (i < 5) { // Mon-Fri
scanf("%d %d", &a, &b);
printf("%d\n", a + b);
} else { // Sat, Sun
scanf("%d %d %d", &a, &b, &c);
printf("%d\n", a + b + c);
}
break;
}
}
}
return 0;
}
O(t * 7) = O(t),常數極小O(1)若追求極致速度,可用 switch 或哈希表(如 unordered_map)替代線性比對,但 t ≤ 100 時線性搜尋完全足夠。