石头-剪刀-布
来源:互联网 发布:swiper vue.js 不兼容 编辑:程序博客网 时间:2024/04/28 21:27
试题六: Rock-Paper-Scissors Tournament
Input:tournament.in output:tournament.out
石头-剪刀-布是两个人玩的游戏。假设有两个人A和B,每个人都独立地选择石头,布或剪刀。选布的赢先石头的,选剪刀的赢选布的,选石头的赢选剪刀的,选相同的既不赢又不输。
输入有多个测试用例。每个测试用例的第一行是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[n];
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;
}
}
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[n];
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;
}
}
- 石头-剪刀-布
- 石头剪刀布
- 剪刀石头布
- 剪刀、石头、布游戏
- 剪刀 石头 布
- 石头剪刀布
- 剪刀石头布
- 石头剪刀布
- 剪刀石头布游戏
- 剪刀石头布
- 剪刀石头布游戏
- 剪刀、石头、布
- 1019:石头剪刀布
- 石头剪刀布1
- 石头-剪刀-布
- 石头剪刀布游戏
- 1019石头、剪刀、布
- 剪刀石头布游戏
- 解决jspsmartupload上传下载的中文乱码问题
- 《Squid 中文权威指南》第2章 译者:彭勇华
- 收集的一些学习网址
- 本机时间显示收集----JS版本
- 什么是极限编程?什么是借口编程?什么是敏捷开发?
- 石头-剪刀-布
- 《Squid 中文权威指南》第3章 译者:彭勇华
- 俄罗斯方块
- asdasdasd
- 《Squid 中文权威指南》第4章 译者:彭勇华
- 《Squid 中文权威指南》第5章 译者:彭勇华
- 另类隐藏文件夹
- 《Squid 中文权威指南》第6章 译者:彭勇华
- 怎么用javascript判断文本框是否为空?