#include <stdio.h>
int main()
{
int t=0,trab=0,T,dtur=0,drab=0
/*定义外界时间t,兔子时间trab,输入时间T,乌龟跑的距离dtur,兔子跑的距离drab*/
int rest=0/*定义休息标识1,初始化为0*/
printf("请输入兔子和乌龟已经跑了的时间:")
scanf("%d",&T)
while(t!=T)
{
dtur+=3//乌龟移动
if(rest!=1)//如果兔子不在休息状态
{
trab++//兔子时间流逝
drab+=9//兔子移动
}
t++//外界时间流逝
if(trab%10==0&&rest==0)
{//如果兔子时间是10的整数倍且兔子不在休息状态,则回头查看和乌龟的距离
if(drab>dtur)//如果兔子在乌龟前面
{
rest=1
trab+=30//兔子休息,兔子时间先增加30,等外界时间追上
}
}
if(t==trab)//如果外界时间增加到和兔子时间一样,则唤醒兔子
rest=0
}
printf("兔子,乌龟分别了%d米,%d米 ---> ",drab,dtur)
if(drab>dtur)
{printf("兔子快!!\n")}
else if(drab<dtur)
{printf("乌龟快!!\n")}
else if(drab==dtur)
{printf("一样快!!\n")}
return 0
}
//当y为5时,兔子往前跑,其余则不动(睡觉)来模拟兔子的行为#include
<iostream>
using
std::cout
using
std::endl
#include
<cstdlib>
#include
<ctime>
#include
<iomanip>
using
std::setw
const
int
RACE_END
=
70
void
moveTortoise(int
*)
void
moveHare(int
*)
void
printCurrentPositions(int
*,int
*)
int
main()
{
int
tortoise
=
1
int
hare
=
1
int
timer
=
0
srand(
time(
0
)
)
cout
<<
"ON
YOUR
MARK,
GET
SET\nBANG
!!!!"//各就各位
<<
"\nAND
THEY
ARE
OFF
!!!!\n"
//
controls
race
while
(
tortoise
!=
RACE_END
&&
hare
!=
RACE_END
)
{
moveTortoise(&tortoise)
moveHare(&hare)
printCurrentPositions(&tortoise,&hare)
++timer
}
//
end
while
//
determine
winner
if
(
tortoise
>=
hare
)
cout
<<
"\nTORTOISE
WINS!!!
YAY!!!\n"
else
cout
<<
"Hare
wins.
Yuch.\n"
cout
<<
"TIME
ELAPSED
=
"
<<
timer
<<
"
seconds"
<<
endl
system("PAUSE")
return
0
}
//
end
main
//
move
tortoise
void
moveTortoise(int
*turtlePtr)
{
int
x
=
1
+
rand()
%
10
//
determine
which
move
to
make
if
(
x
>=
1
&&
x
<=
5
)
//
fast
plod(快走)
*turtlePtr
+=
3
else
if
(
x
==
6
||
x
==
7
)
//
slip(滑倒)
*turtlePtr
-=
6
else
//
slow
plod(慢走)
++(
*turtlePtr
)
//
ensure
that(确保)
tortoise
remains
within
subscript
range(下标)
if
(
*turtlePtr
<
1
)
*turtlePtr
=
1
else
if
(
*turtlePtr
>
RACE_END
)
*turtlePtr
=
RACE_END
}
//
end
function
moveTortoise
//
move
hare
void
moveHare(int
*harePtr)
{
int
y
=
1
+
rand()
%
10
if
(y==5)
*harePtr+=8
if
(*harePtr
<1)
*harePtr=1
else
if(
*harePtr
>
RACE_END
)
*harePtr
=
RACE_END
}
//
end
function
moveHare
//
output
positions
of
animals
void
printCurrentPositions(int
*snapperPtr,int
*bunnyPtr)
{
if
(
*bunnyPtr
==
*snapperPtr
)
//bunny:小兔子
cout
<<
setw(
*bunnyPtr
)
<<
"OUCH!!!"
else
if
(
*bunnyPtr
<
*snapperPtr
)
cout
<<
setw(
*bunnyPtr
)
<<
"H"
<<
setw(
*snapperPtr
-
*bunnyPtr
)
<<
"T"
else
cout
<<
setw(
*snapperPtr
)
<<
"T"
<<
setw(
*bunnyPtr
-
*snapperPtr
)
<<
"H"
cout
<<
"\n"
}
//
end
function
printCurrentPositions
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)