主题:1.火柴常胜将军问题 改写C++
mysong
[专家分:0] 发布于 2010-05-07 19:51:00
1.火柴常胜将军问题
#include<stdio.h>
void main()
{
int a=21,i;
printf("Game begin:\n");
while(a>0)
{
do{
printf("How many stick do you wish to take(1~%d)?",a>4?4:a);
scanf("%d",&i);
}while(i>4||i<1||i>a); /*接收正在确的输入*/
if(a-i>0) printf(" %d stick left in the pile.\n",a-i);
if((a-i)<=0)
{
printf(" You have taken the last stick.\n");
printf(" * * * You lose! \nGame Over.\n"); /*输出取胜标记*/
break;
}
else
printf(" Compute take %d stick.\n",5-i); /*输出计算机取的子数*/
a-=5;
printf(" %d stick left in the pile.\n",a);
}
}
沙发
senlin8350 [专家分:10] 发布于 2010-05-08 22:29:00
#include <iostream>
using namespace std;
int main()
{
int a=21,i;
cout<<"Game begin:\n";
while(a>0){
do{cout<<"How many stick do you wish to take?"<<endl;
cin>>i;
}while(i>4||i<1||i>a); /*接收正在确的输入*/
if(a-i>0)
cout<<"stick left in the pile"<<'\n'<<a-i;
if((a-i)<=0)
{
cout<<" You have taken the last stick"<<'\n';
cout<<" * * * You lose! Game Over.\n";
/*输出取胜标记*/
break;
}
else
cout<<" Compute take stick\n"<<5-i;
cout<<endl;
/*输出计算机取的子数*/
a-=5;
cout<<"stick left in the pile.\n"<<a;
cout<<endl;
}
return 0;
}[em2]