请参考以下代码,实现对戴帽子的颜色进行判断:
#include <stdio.h>
int main() {
int hatColors[5] = {0}; // 存储每个人戴的帽子颜色,0表示未确定,1表示白帽子,2表示红帽子
for (int i = 1; i <= 5; i++) {
hatColors[i-1] = 1; // 假设第i个人戴白帽子
if (hatColors[0] != 1) { // 如果甲看到的不是白帽子,则假设错误
hatColors[i-1] = 2;
}
if (hatColors[3] == 1) { // 如果丁看到了白帽子,则假设错误
hatColors[i-1] = 2;
}
if ((hatColors[2] == 1 && i != 3) || (hatColors[2] != 1 && i == 3)) { // 如果丙看到的是白帽子,并且不是甲,则假设错误;或者如果丙看到的不是白帽子,并且是甲,则假设错误
hatColors[i-1] = 2;
}
if ((i == 4 && hatColors[4] != hatColors[2]) || (i !=4 && hatColors[4]==hatColors[2])) { // 如果戊和丙的帽子颜色相同,则假设正确;否则假设错误
hatColors[i-1] = 2;
}
}
// 输出结果
printf("甲戴%s帽子\n", hatColors[0]==1 ? "白" : "红");
printf("乙戴%s帽子\n", hatColors[1]==1 ? "白" : "红");
printf("丙戴%s帽子\n", hatColors[2]==1 ? "白" : "红");
printf("丁戴%s帽子\n", hatColors[3]==1 ? "白" : "红");
printf("戊戴%s帽子\n", hatColors[4]==1 ? "白" : "红");
return 0;
}
根据题目中的线索进行逻辑判断,最后输出每个人所戴的帽子颜色。
内容由零声教学AI助手提供,问题来源于学员提问




