给定一定数量的苹果,谁取了最后一个苹果就算谁赢。用C语言编写

来源:百度知道 编辑:UC知道 时间:2024/06/23 16:57:57
游戏规则是:人和电脑双方轮流取苹果;第一次无论哪一方先取苹果,都只能取“1~总苹果数目-1”之间(包括边界)数目的苹果;之后的每一方取苹果的数目只能是“1~上一次对方取苹果数目的两倍”之间的苹果个数;直至某一方将苹果取完为止,游戏才告结束。
这样有什么意思啊 我的意思要电脑能自己取啊这是一个费波那契数列的应用程序

#include <stdio.h>
#include <stdlib.h>

int main()
{
int total;
int people, computer;
int flag;
int i;

printf("输入苹果数:");
scanf("%d", &total);
i = rand() % 2; // 随机生成一个数让它除以2,除数为0,人先取,否则电脑先取。

if (i == 0){
printf("人先取,输入个数:\n");
scanf("%d", &people);
if (people < 1 || people > total - 1){
do{
printf("输入的个数应该在1-%d之间,重新输入:", total - 1);
scanf("%d", &people);
}while(people < 1 || people > total - 1);
flag = 2;
total = total - people;
// printf("还剩%d个\n", total);
}
}
else{
printf("电脑先取,输入个数:\n");
scanf("%d", &computer);
if (computer < 1 || computer > total - 1){
do{
printf("输入的个数应该在1-%d之间,重新输入:", total - 1);
scanf("%d"