急求编写一个进行人机对弈的程序

来源:百度知道 编辑:UC知道 时间:2024/06/06 15:12:50
程序如下二十一根火柴两个人轮流取,每人每次取一到四根.不可多取也不能不取.谁取最后一根谁输,要求人先取,计算机后取,计算机常胜.

#include<stdio.h>
int f(int n){
switch(n){
case 0:return 4;
case 4:return 3;
case 3:return 2;
case 2:return 1;
}
return 0;
}
void main(){
int m=21,n;
do{
scanf("%d",&n);
m-=n;
n=f(m%5);
printf ("%d\n",n);
m-=n;
}while(m>1);
}

其实有更简单的,如下:
#include<stdio.h>
void main(){
int n;
do{
scanf("%d",&n);
printf ("%d\n",5-n);
}while(1);
}