石头-剪刀-布
2007-08-06 16:52
393 查看
试题六: Rock-Paper-Scissors Tournament
Input:tournament.in output:tournament.out
石头-剪刀-布是两个人玩的游戏。假设有两个人A和B,每个人都独立地选择石头,布或剪刀。选布的赢先石头的,选剪刀的赢选布的,选石头的赢选剪刀的,选相同的既不赢又不输。
n个人参加,每个人与其他每个人比k轮石头-布-剪刀游戏,总共要赛k*n*(n-1)/2场。你的工作是计算每个人赢的平均数,这个平均数定义为w/(w+l),其中w是赢的场数,而l是输的场数。
输入有多个测试用例。每个测试用例的第一行是n,k(1 ≤ n ≤ 100 ,1 ≤ k ≤ 100),n是参加的人数,k是每个人比赛的场数。对每场比赛,由包含四个整数的一行表示:p1,m1,p2,m2.其中1<=p1<=n,1<=p2<=n表示比赛的两人,m1,m2分别是他们出的手势(”rock”,”scissors”,或”paper”)。最后以一个0表示输入结束。
对每个参加者各输出一行,给出他们赢的平均场数,精确到小数点后三位。如果赢的平均场数不定,则输出”-“。两测试用例间输出一空行。
Sample Input
2 4
1 rock 2 paper
1 scissors 2 paper
1 rock 2 rock
2 rock 1 scissors
2 1
1 rock 2 paper
0
Out put for Sample Input
0.333
0.667
0.000
1.000
import java.io.*;
import java.util.*;
import java.text.*;
class player
...{
public double total;
public double fail;
public int number;
public player(double total,int number,double fail)
...{
this.total=total;
this.number=number;
this.fail=fail;
}
}
class infor
...{
public int p1;//选手
public String s1;//策略
public int p2;
public String s2;
public infor(int p1,String s1,int p2,String s2)
...{
this.p1=p1;
this.s1=s1;
this.p2=p2;
this.s2=s2;
}
}
public class Test6
...{
public static void main(String[] args) throws Exception
...{
BufferedReader bf=new BufferedReader(new FileReader("tournament.in"));
StringTokenizer tag;
String str;
DecimalFormat f=new DecimalFormat("0.000");
while(!(str=bf.readLine()).equals("0"))
...{
tag=new StringTokenizer(str);
int n=Integer.parseInt(tag.nextToken());
int k=Integer.parseInt(tag.nextToken());
player p[]=new player
;
for(int i=0;i<n;i++)
p[i]=new player(0,i+1,0);
infor m[]=new infor[k*n*(n-1)/2];
for(int i=0;i<k*n*(n-1)/2;i++)
...{
tag=new StringTokenizer(bf.readLine());
m[i]=new infor(Integer.parseInt(tag.nextToken()),tag.nextToken(),Integer.parseInt(tag.nextToken()),tag.nextToken());
}
int tmp;
for(int i=0;i<k*n*(n-1)/2;i++)
...{
tmp=getWinner(m[i],p);
if(tmp==0)continue;
else p[tmp-1].total++;
}
for(int i=0;i<n;i++)
System.out.println(f.format(p[i].total/(p[i].total+p[i].fail)));
}
}
public static int getWinner(infor m,player p[])
...{
if(m.s1.equals("rock")&&m.s2.equals("scissors"))
...{
p[m.p2-1].fail++;
return m.p1;
}
else if(m.s1.equals("scissors")&&m.s2.equals("rock"))
...{
p[m.p1-1].fail++;
return m.p2;
}
else if(m.s1.equals("rock")&&m.s2.equals("paper"))
...{
p[m.p1-1].fail++;
return m.p2;
}
else if(m.s1.equals("paper")&&m.s2.equals("rock"))
...{
p[m.p2-1].fail++;
return m.p1;
}
else if(m.s1.equals("paper")&&m.s2.equals("scissors"))
...{
p[m.p1-1].fail++;
return m.p2;
}
else if(m.s1.equals("scissors")&&m.s2.equals("paper"))
...{
p[m.p2-1].fail++;
return m.p1;
}
else
return 0;
}
}
Input:tournament.in output:tournament.out
石头-剪刀-布是两个人玩的游戏。假设有两个人A和B,每个人都独立地选择石头,布或剪刀。选布的赢先石头的,选剪刀的赢选布的,选石头的赢选剪刀的,选相同的既不赢又不输。
n个人参加,每个人与其他每个人比k轮石头-布-剪刀游戏,总共要赛k*n*(n-1)/2场。你的工作是计算每个人赢的平均数,这个平均数定义为w/(w+l),其中w是赢的场数,而l是输的场数。
输入有多个测试用例。每个测试用例的第一行是n,k(1 ≤ n ≤ 100 ,1 ≤ k ≤ 100),n是参加的人数,k是每个人比赛的场数。对每场比赛,由包含四个整数的一行表示:p1,m1,p2,m2.其中1<=p1<=n,1<=p2<=n表示比赛的两人,m1,m2分别是他们出的手势(”rock”,”scissors”,或”paper”)。最后以一个0表示输入结束。
对每个参加者各输出一行,给出他们赢的平均场数,精确到小数点后三位。如果赢的平均场数不定,则输出”-“。两测试用例间输出一空行。
Sample Input
2 4
1 rock 2 paper
1 scissors 2 paper
1 rock 2 rock
2 rock 1 scissors
2 1
1 rock 2 paper
0
Out put for Sample Input
0.333
0.667
0.000
1.000
import java.io.*;
import java.util.*;
import java.text.*;
class player
...{
public double total;
public double fail;
public int number;
public player(double total,int number,double fail)
...{
this.total=total;
this.number=number;
this.fail=fail;
}
}
class infor
...{
public int p1;//选手
public String s1;//策略
public int p2;
public String s2;
public infor(int p1,String s1,int p2,String s2)
...{
this.p1=p1;
this.s1=s1;
this.p2=p2;
this.s2=s2;
}
}
public class Test6
...{
public static void main(String[] args) throws Exception
...{
BufferedReader bf=new BufferedReader(new FileReader("tournament.in"));
StringTokenizer tag;
String str;
DecimalFormat f=new DecimalFormat("0.000");
while(!(str=bf.readLine()).equals("0"))
...{
tag=new StringTokenizer(str);
int n=Integer.parseInt(tag.nextToken());
int k=Integer.parseInt(tag.nextToken());
player p[]=new player
;
for(int i=0;i<n;i++)
p[i]=new player(0,i+1,0);
infor m[]=new infor[k*n*(n-1)/2];
for(int i=0;i<k*n*(n-1)/2;i++)
...{
tag=new StringTokenizer(bf.readLine());
m[i]=new infor(Integer.parseInt(tag.nextToken()),tag.nextToken(),Integer.parseInt(tag.nextToken()),tag.nextToken());
}
int tmp;
for(int i=0;i<k*n*(n-1)/2;i++)
...{
tmp=getWinner(m[i],p);
if(tmp==0)continue;
else p[tmp-1].total++;
}
for(int i=0;i<n;i++)
System.out.println(f.format(p[i].total/(p[i].total+p[i].fail)));
}
}
public static int getWinner(infor m,player p[])
...{
if(m.s1.equals("rock")&&m.s2.equals("scissors"))
...{
p[m.p2-1].fail++;
return m.p1;
}
else if(m.s1.equals("scissors")&&m.s2.equals("rock"))
...{
p[m.p1-1].fail++;
return m.p2;
}
else if(m.s1.equals("rock")&&m.s2.equals("paper"))
...{
p[m.p1-1].fail++;
return m.p2;
}
else if(m.s1.equals("paper")&&m.s2.equals("rock"))
...{
p[m.p2-1].fail++;
return m.p1;
}
else if(m.s1.equals("paper")&&m.s2.equals("scissors"))
...{
p[m.p1-1].fail++;
return m.p2;
}
else if(m.s1.equals("scissors")&&m.s2.equals("paper"))
...{
p[m.p2-1].fail++;
return m.p1;
}
else
return 0;
}
}
相关文章推荐
- Bzoj2597 [Wc2007]剪刀石头布
- 16:矩阵剪刀石头布
- 简单小游戏-剪刀石头布的c语言实现
- 代码制作:剪刀、石头、布游戏
- 关于php中剪刀石头布的游戏!高手看一下有啥问题吗?
- BZOJ 2597 [Wc2007]剪刀石头布
- 剪刀、石头、布
- 剪刀石头布
- 剪刀石头布
- 石头,剪刀,布游戏
- 剪刀石头布常胜秘笈
- BZOJ 2597 WC2007 剪刀石头布 费用流
- 剪刀石头布小习题三种语言python2、php、go代码
- 寒風的Cocos2dx之旅之剪刀、石头、布系列专题(2 )
- OJ题——剪刀、石头和布
- Java猜拳小游戏(剪刀、石头、布)
- 8,石头,剪刀,布的逻辑代码
- 2516: 剪刀石头布
- [bzoj2597][WC2007]剪刀石头布
- CSU 1202 剪刀石头布