请帮我修改一下人狼羊菜过河问题的代码
时间: 2023-03-16 18:44:02 浏览: 226
用python编写的一款小游戏,实现人狼羊菜过河
很高兴为您服务,我尽力给您修改一下人狼羊菜过河问题的代码。请根据您的需求,修改以下代码:// 人狼羊菜过河问题
int main()
{
// 定义变量
int farmer = 0;
int wolf = 0;
int goat = 0;
int cabbage = 0;
while (farmer != 1 || wolf != 1 || goat != 1 || cabbage != 1)
{
// 过河前
printf("Farmer: %d, Wolf: %d, Goat: %d, Cabbage: %d\n", farmer, wolf, goat, cabbage);
int choice;
printf("请输入您要搭乘的物品编号:\n");
scanf("%d", &choice);
// 过河后
switch (choice)
{
case 0:
farmer = 1 - farmer;
break;
case 1:
wolf = 1 - wolf;
break;
case 2:
goat = 1 - goat;
break;
case 3:
cabbage = 1 - cabbage;
break;
}
if ((wolf == goat && farmer == 0) || (goat == cabbage && farmer == 0))
{
printf("无法完成!过河失败!\n");
break;
}
}
if (farmer == 1 && wolf == 1 && goat == 1 && cabbage == 1)
{
printf("过河成功!\n");
}
return 0;
}
阅读全文