作者在 2017-04-08 14:27:28 发布以下内容
#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
const int SHIP=1;
const int BAR=2;
const int WATER=3;
int drunkard(void)
{
int x=-10; //酒者一开始在酒馆
int y=0; //酒者一开始在跳板中央
int step=0; //初始化酒者的步数
while(abs(x)<=10&&abs(y)<=2)
{
switch(rand()%10)
{
case 0:
y--; break; //向左走
case 1:
y++; break; //向右走
case 2:
x--; break; //向后
default:
x++; break; //向前
}
step++;
}
if(x<-10&&abs(y)<=2) //在跳板内返回酒吧
{
cout <<"经过" <<step <<"步后drunkard返回酒吧" <<endl ;
return BAR;
}
else
{
if(x>10&&abs(y)<=2) //在跳板内返回船
{
cout <<"经过" <<step <<"步后drunkard返回船上" <<endl ;
return SHIP;
}
else //y>2时表示为落水
{
cout <<"经过" <<step <<"步后drunkard掉进水里" <<endl ; //
return WATER;
}
}
}
int main()
{
int numbdrunkard;
int backship=0; //初始化很重要,不然数据会不准确
int backbar=0;
int dropwater=0;
cout <<"请输入drunkard的数量:";
cin >>numbdrunkard ;
for(int i=0;i<numbdrunkard;i++)
{
switch(drunkard()) //计数
{
case SHIP:
backship++; break;
case BAR:
backbar++; break;
case WATER:
dropwater++; break;
}
}
cout <<"——————————————————" <<endl ;
cout <<backship <<"个drunkard回到船中 " <<endl ;
cout <<backbar <<"个drunkard回到酒吧 " <<endl ;
cout <<dropwater <<"个drunkard落到水中 " <<endl ;
return 0;
#include<cmath>
#include<cstdlib>
using namespace std;
const int SHIP=1;
const int BAR=2;
const int WATER=3;
int drunkard(void)
{
int x=-10; //酒者一开始在酒馆
int y=0; //酒者一开始在跳板中央
int step=0; //初始化酒者的步数
while(abs(x)<=10&&abs(y)<=2)
{
switch(rand()%10)
{
case 0:
y--; break; //向左走
case 1:
y++; break; //向右走
case 2:
x--; break; //向后
default:
x++; break; //向前
}
step++;
}
if(x<-10&&abs(y)<=2) //在跳板内返回酒吧
{
cout <<"经过" <<step <<"步后drunkard返回酒吧" <<endl ;
return BAR;
}
else
{
if(x>10&&abs(y)<=2) //在跳板内返回船
{
cout <<"经过" <<step <<"步后drunkard返回船上" <<endl ;
return SHIP;
}
else //y>2时表示为落水
{
cout <<"经过" <<step <<"步后drunkard掉进水里" <<endl ; //
return WATER;
}
}
}
int main()
{
int numbdrunkard;
int backship=0; //初始化很重要,不然数据会不准确
int backbar=0;
int dropwater=0;
cout <<"请输入drunkard的数量:";
cin >>numbdrunkard ;
for(int i=0;i<numbdrunkard;i++)
{
switch(drunkard()) //计数
{
case SHIP:
backship++; break;
case BAR:
backbar++; break;
case WATER:
dropwater++; break;
}
}
cout <<"——————————————————" <<endl ;
cout <<backship <<"个drunkard回到船中 " <<endl ;
cout <<backbar <<"个drunkard回到酒吧 " <<endl ;
cout <<dropwater <<"个drunkard落到水中 " <<endl ;
return 0;
}