CrossChao's Code
I'm thinking
C++博客
::
首页
::
新随笔
::
联系
::
聚合
::
管理
::
1 随笔 :: 3 文章 :: 1 评论 :: 0 Trackbacks
<
2026年6月
>
日
一
二
三
四
五
六
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
常用链接
我的随笔
我的评论
我参与的随笔
留言簿
给我留言
查看公开留言
查看私人留言
随笔档案
2009年7月 (1)
文章档案
2009年8月 (2)
2009年7月 (1)
搜索
最新随笔
1. 埃及分数 - 完成中········
最新评论
1. re: 剪刀石头布
这代码好天真,只要生成了不是3的倍数的环,裁判就存在。怎么会是那么简单的判断?
--ylc
测试代码效果
1
#include
<
iostream
>
2
using
namespace
std;
3
4
struct
rubbish
//
垃圾
5
{
6
int
time;
//
投入现陷阱中的时间
7
int
life;
//
能维持生命的时间
8
int
height;
//
垃圾的高度
9
bool
is_in;
//
此垃圾是否已经投入进去
10
rubbish
*
next;
//
指向垃圾队列中下一个垃圾的指针
11
}
;
12
13
class
Cow
14
{
15
public
:
16
Cow();
17
private
:
18
int
height;
//
陷阱的深度
19
int
numRubbish;
//
陷阱中垃圾的数量
20
rubbish
*
phead;
//
指向垃圾队列的头指针
21
int
currentTime;
//
当前时间
22
}
;
23
24
Cow::Cow()
25
{
26
cout
<<
"
请输入陷阱的深度和陷阱中垃圾的数量
"
<<
endl;
27
cin
>>
this
->
height
>>
this
->
numRubbish;
28
cout
<<
"
请输入
"
<<
this
->
numRubbish
<<
"
个垃圾的信息
"
<<
endl;
29
30
this
->
phead
=
new
rubbish;
31
cin
>>
this
->
phead
->
time
>>
this
->
phead
->
life
>>
this
->
phead
->
height;
32
this
->
phead
->
is_in
=
false
;
33
34
rubbish
*
pTmp
=
this
->
phead;
35
36
for
(
int
i
=
1
; i
<
this
->
numRubbish;
++
i )
37
{
38
pTmp
->
next
=
new
rubbish;
39
cin
>>
pTmp
->
next
->
time
>>
pTmp
->
next
->
life
>>
pTmp
->
next
->
height;
40
pTmp
->
next
->
is_in
=
false
;
41
pTmp
->
next
->
next
=
NULL;
42
43
pTmp
=
pTmp
->
next;
44
}
45
pTmp
=
this
->
phead;
46
cout
<<
"
垃圾堆列为:
"
<<
endl;
47
for
( i
=
0
; i
<
this
->
numRubbish;
++
i )
48
{
49
cout
<<
"
(
"
<<
pTmp
->
time
<<
"
,
"
<<
pTmp
->
life
<<
"
,
"
<<
pTmp
->
height
<<
"
)
"
<<
pTmp
->
is_in
<<
"
"
;
50
pTmp
=
pTmp
->
next;
51
}
52
cout
<<
endl;
53
54
this
->
currentTime
=
0
;
//
当前时间
55
int
currentHeight
=
0
;
//
当前垃圾垫起来的高度
56
int
cowsLife
=
10
;
//
母牛的生命
57
while
(
++
this
->
currentTime )
//
开始计时
58
{
59
//
system("pause");
60
61
pTmp
=
this
->
phead;
//
指向第一个垃圾
62
63
for
( i
=
0
; i
<
this
->
numRubbish;
++
i )
64
{
65
if
( pTmp
->
is_in
==
false
&&
pTmp
->
time
==
this
->
currentTime )
//
检查此垃圾是否可以投入陷阱内
66
{
67
//
cout << "Debug:投入了一个垃圾,当前的时间为" << currentTime << endl;
68
pTmp
->
is_in
=
true
;
//
投入此垃圾
69
currentHeight
+=
pTmp
->
height;
//
修改陷阱中垃圾的高度
70
}
71
//
cout << "Debug: " << pTmp->is_in << endl;
72
//
cout << "Debug::刚才检查高度为:" << pTmp->height << "的垃圾是否投入" << endl;
73
pTmp
=
pTmp
->
next;
74
}
75
76
pTmp
=
this
->
phead;
//
恢复指针指向第一个垃圾
77
rubbish
*
pMinHeight
=
NULL;
//
指向高度最小的垃圾
78
int
rubbishHeight
=
25
;
//
垃圾高度最小值的初始值25
79
80
if
( cowsLife
==
-
1
)
//
如果母牛该吃东西了
81
{
82
//
cout << "Debug:母牛开始寻找食物进食,当前的垃圾高度为: " << currentHeight << endl;
83
for
( i
=
0
; i
<
this
->
numRubbish;
++
i )
//
便利垃圾队列
84
{
85
if
( pTmp
->
is_in
==
true
&&
pTmp
->
height
<
rubbishHeight )
86
{
87
//
cout << "Debug::找到了一个合适的也许能吃的垃圾,高度为: " << pTmp->height << endl;
88
rubbishHeight
=
pTmp
->
height;
89
pMinHeight
=
pTmp;
90
}
91
//
cout << "Debug:寻找垃圾队列的便利" << endl;
92
pTmp
=
pTmp
->
next;
//
指针迭进
93
}
94
//
此时要吃的垃圾已经选出来了,如果pMinHeight为空则说明已经没有垃圾可以吃了
95
if
( pMinHeight
!=
NULL )
//
如果还有垃圾可以吃,则吃掉这个垃圾
96
{
97
//
cout << "Debug::母牛吃进去的垃圾的高度为 : " << pMinHeight->height << endl;
98
cowsLife
+=
pMinHeight
->
life;
//
吃后母牛声明增加
99
pMinHeight
->
is_in
=
false
;
//
从陷阱中取出这个垃圾
100
//
开始删除这个已经被母牛吃了的垃圾,这里的删除并不实际在垃圾队列中删除,只是减小一下陷阱中总垃圾的高度 -
101
//
- 因为这个垃圾应不能在被投入陷阱中了,故不必真正的垃圾队列中删除
102
currentHeight
-=
pMinHeight
->
height;
//
总高度减去这个已经被吃掉的垃圾的高度
103
}
104
}
105
106
if
( currentHeight
>=
this
->
height )
//
如果垃圾总高度不小于陷阱的高度 则成功就出母牛
107
{
108
cout
<<
"
母牛成功在
"
<<
this
->
currentTime
<<
"
救出
"
<<
endl;
109
return
;
110
}
111
else
if
( cowsLife
<=
-
1
)
//
检查母牛是否或者
112
{
113
cout
<<
"
母牛未能成功救出,在
"
<<
this
->
currentTime
<<
"
时死亡
"
<<
endl;
114
return
;
115
}
116
117
--
cowsLife;
//
母牛生命减少
118
}
119
}
120
121
int
main()
122
{
123
Cow cow1;
124
return
0
;
125
}
posted on 2009-07-25 14:17
CrossChao
阅读(97)
评论(0)
编辑
收藏
引用
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
博客园最新博文
博问
管理
Powered by:
C++博客
Copyright © CrossChao