您的位置:首页 > 其它

石头-剪刀-布

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;


    }


}



 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息