源码
#include<iostream.h>
void main()
{
int a=21, i;
cout<<"----------------------------------Game begin----------------------------------"<<endl;
cout<<"题目:"<<endl;
cout<<"现有21根火柴,每人轮流取,每人每次可以取1至4根,不可多取,也不能不取,谁取最后一根火柴谁输。"<<endl;
cout<<"你先:"<<endl;
while(a>1)
{
cout<<"你想取多少根火柴?"<<endl;
cin>>i;
if(i<1 || i>4)
{
cout<<"数据错误,请再次输入。"<<endl;
}
else
{
cout<<"剩下"<<a-i<<"根火柴。"<<endl;
cout<<"计算机取了"<<5-i<<"根火柴。"<<endl;
a=a-5;
cout<<"剩下"<<a<<"根火柴。"<<endl;
}
}
cout<<"你想取多少根火柴?"<<endl;
cin>>i;
if(i!=1)
{
cout<<"数据错误,请再次输入。"<<endl;
cout<<"你想取多少根火柴?"<<endl;
cin>>i;
}
else
{
cout<<"你取了最后一根火柴。"<<endl;
}
cout<<"你输了!"<<endl;
cout<<"----------------------------------Game Over----------------------------------"<<endl;
cout<<"按下任何一个按键,程序退出。"<<endl;
}
效果