C语言-常胜将军

现在有21根火柴,2人轮流取,每人每次可以取走1~4根,不可多取,也不能不取,谁取最后一根火柴谁输。编写一个程序进行人机对弈,要求人先取,计算机后取;计算机一方为 常胜将军。

int main(int argc, const char *argv[]) {
    int computer, people, spare = 21;
    printf("-------------------------------\n");
    printf("---------------begin----------------\n");
    printf("-------------------------------\n");
    while (1) {
        printf("---------------- 目前还有火柴%d根--------------\n", spare);
        printf("People:");
        scanf("%d", &people);
        if (people < 1 || people > 4 || people > spare) {
            printf("输入违规,取得数目有问题!\n\n");
            continue;
        }
        spare -= people;
        if (spare == 0) {
            printf("computer win !Game Over!");
            break;
        }
        computer = 5 - people;
        spare -= computer;
        printf("Computer: %d    \n", computer);
        if (spare == 0) {
            printf("\nPeople win!Game Over!\n");
            break;
        }
    }
 return 0;
}

C语言-常胜将军_第1张图片

你可能感兴趣的:(数据结构与算法)