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
剪刀石头布
/**/
/*
剪刀石头布
*/
#include
<
iostream
>
#include
<
vector
>
#include
<
string
>
using
namespace
std;
struct
numNod
{
int
value;
//
小孩的编号
vector
<
int
>
win;
//
胜出的数字队列 暂定20个 以后编写动态增加数组后再修改
vector
<
int
>
lose;
//
输掉的数字队列 暂定20个 以后编写动态增加数组后再修改
numNod
*
next;
}
;
//
如果某个数字结构体的胜出队列中和失败队列中出现二次或者两次以上的同数字结果则说明这个数字是裁判
//
另外值得注意的是只有裁判才会出现平局的情况
class
Run
{
public
:
Run();
int
Compare(
int
num1,
int
num2,
char
sign );
private
:
numNod
*
p;
int
CheckUp();
//
检查函数,检查裁判是否已经得出
int
numOfCheck;
//
猜拳进行的次数
numNod
*
SearchNum(
int
num );
//
查找p所指向队列是否有num这个数字结构,有的话返回指向num的地址,没有的话返回的地址是NULL
void
ReworkList( numNod
*
p,
char
sign,
int
num2 );
//
第一种情况,给出指向数字结构体指针,输赢标志(1,2,3),比较对象(数字)
void
ReworkList(
int
num1,
char
sign,
int
num2 );
//
第二种情况,即结构体数字队列没有这个数字要重新生成
}
;
Run::Run()
{
this
->
p
=
NULL;
this
->
numOfCheck
=
0
;
}
numNod
*
Run::SearchNum(
int
num )
{
if
(
!
this
->
p )
{
return
NULL;
}
else
{
numNod
*
pTmp
=
this
->
p;
while
( pTmp
->
value
!=
num
&&
pTmp
->
next )
pTmp
=
pTmp
->
next;
//
至此查找完毕开始检测是否已经找到
if
( pTmp
->
value
==
num )
{
return
pTmp;
}
else
{
return
NULL;
}
}
}
void
Run::ReworkList( numNod
*
p,
char
sign,
int
num2 )
{
if
( sign
==
'
>
'
)
{
p
->
win.push_back( num2 );
}
else
if
( sign
==
'
<
'
)
{
p
->
lose.push_back( num2 );
}
else
if
( sign
==
'
=
'
)
//
平局
{
p
->
win.push_back( num2 );
p
->
lose.push_back( num2 );
}
else
//
非法比较字符
{
cout
<<
"
Sign Error!
"
<<
endl;
return
;
}
}
void
Run::ReworkList(
int
num1,
char
sign,
int
num2 )
{
numNod
*
pTmp
=
this
->
p;
if
(
this
->
p
==
NULL )
{
this
->
p
=
new
numNod;
this
->
p
->
next
=
NULL;
this
->
p
->
value
=
num1;
pTmp
=
this
->
p;
}
else
{
while
( pTmp
->
next )
pTmp
=
pTmp
->
next;
pTmp
->
next
=
new
numNod;
pTmp
->
next
->
value
=
num1;
pTmp
->
next
->
next
=
NULL;
pTmp
=
pTmp
->
next;
}
if
( sign
==
'
>
'
)
{
pTmp
->
win.push_back( num2 );
}
else
if
( sign
==
'
<
'
)
{
pTmp
->
lose.push_back( num2 );
}
else
if
( sign
==
'
=
'
)
//
平局
{
pTmp
->
win.push_back( num2 );
pTmp
->
lose.push_back( num2 );
}
else
//
非法比较字符
{
cout
<<
"
Sign Error!
"
<<
endl;
return
;
}
}
int
Run::Compare(
int
num1,
int
num2,
char
sign )
{
numNod
*
pTmp;
int
result(
-
1
);
//
检查队列有没有num1 和 num2 有点话处理 每有生成
pTmp
=
this
->
SearchNum( num1 );
if
( pTmp )
//
如果已经有
{
this
->
ReworkList( pTmp, sign, num2 );
}
else
{
this
->
ReworkList( num1, sign, num2 );
}
//
处理第二个数字
pTmp
=
this
->
SearchNum( num2 );
if
( pTmp )
//
如果已经有
{
this
->
ReworkList( pTmp, sign, num1 );
}
else
{
this
->
ReworkList( num2, sign, num1 );
}
++
this
->
numOfCheck;
result
=
this
->
CheckUp();
if
( result
!=
-
1
)
{
return
result;
}
return
-
1
;
}
int
Run::CheckUp()
//
返回-1表示没有得出裁判,否则返回裁判的编号
{
numNod
*
pTmp
=
this
->
p;
int
numOfSame
=
0
;
//
win lose列有相同有相同数字的次数
while
( pTmp )
{
for
( vector
<
int
>
::size_type i
=
0
; i
<
pTmp
->
win.size();
++
i )
{
for
( vector
<
int
>
::size_type j
=
0
; j
<
pTmp
->
lose.size();
++
j )
{
if
( pTmp
->
win[i]
==
pTmp
->
lose[j] )
++
numOfSame;
}
}
if
( numOfSame
>
1
)
{
return
pTmp
->
value;
//
返回裁判数字编号
}
numOfSame
=
0
;
pTmp
=
pTmp
->
next;
}
return
-
1
;
}
int
main()
{
Run example1;
cout
<<
"
请输入选手人数 和 比赛次数
"
<<
endl;
int
num1(
0
), num2(
0
);
cin
>>
num1
>>
num2;
if
( num1
==
1
&&
num2
==
0
)
{
cout
<<
"
Player 0 can be determined to be the judge after 0 lines
"
<<
endl;
return
0
;
}
else
if
( num2
==
0
&&
num1
>
1
)
{
cout
<<
"
Can not determine
"
<<
endl;
return
0
;
}
cout
<<
"
请输入
"
<<
num2
<<
"
次的比赛结果,让程序来猜测裁判是谁,输入格式例如 1 < 0
"
<<
endl;
int
num3(
0
), num4(
0
);
char
sign;
//
string string1;
int
comparlines(
0
);
while
( cin
>>
num3
>>
sign
>>
num4
&&
comparlines
<
num2 )
{
//
cout << "Debug:: string1:" << string1 << endl;
//
num3 = string1[0];
//
sign = string1[1];
//
num4 = string1[2];
//
cout << "Debug:: num3" << string1[0] << endl;
if
( num3
<
0
||
num3
>
num1
||
num4
<
0
||
num4
>
num1 )
{
cout
<<
"
存在选手编号非法输入,程序退出!
"
<<
endl;
return
-
1
;
}
int
rs
=
example1.Compare( num3, num4, sign );
if
( rs
!=
-
1
)
{
cout
<<
"
Player
"
<<
rs
<<
"
can be determined to be the judge after
"
<<
comparlines
<<
"
lines
"
<<
endl;
return
0
;
}
++
comparlines;
if
( comparlines
==
num2 )
{
cout
<<
"
Can not determine
"
<<
endl;
return
0
;
}
}
return
0
;
}
posted on 2009-08-27 19:27
CrossChao
阅读(486)
评论(1)
编辑
收藏
引用
评论
#
re: 剪刀石头布
2011-07-17 17:47
ylc
这代码好天真,只要生成了不是3的倍数的环,裁判就存在。怎么会是那么简单的判断?
回复
更多评论
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
博客园最新博文
博问
管理
Powered by:
C++博客
Copyright © CrossChao