您的位置:首页 > 编程语言 > Java开发

华为机试---钓鱼比赛

2016-06-22 11:09 441 查看

题目描述

ss请cc来家里钓鱼,鱼塘可划分为n*m的格子,每个格子每分钟有不同的概率钓上鱼,cc一直在坐标(x,y)的格子钓鱼,而ss每分钟随机钓一个格子。问t分钟后他们谁至少钓到一条鱼的概率大?为多少?

[b]输入描述:[/b]
第一行五个整数n,m,x,y,t(1≤n,m,t≤1000,1≤x≤n,1≤y≤m);
接下来为一个n*m的矩阵,每行m个一位小数,共n行,第i行第j个数代表坐标为(i,j)的格子钓到鱼的概率为p(0≤p≤1)


[b]输出描述:[/b]
输出两行。第一行为概率大的人的名字(cc/ss/equal),第二行为这个概率(保留2位小数)


[b]输入例子:[/b]
2 2 1 1 1
0.2 0.1
0.1 0.4


[b]输出例子:[/b]
equal
0.20

import java.util.Scanner;
//逐行读取才不会超时,注意字符串格式化输出printf 或者String.format 
public class Main {
 public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){
         String[] s = scan.nextLine().split(" ");
            int n = Integer.parseInt(s[0]);
            int m = Integer.parseInt(s[1]);
            int x = Integer.parseInt(s[2]);
            int y = Integer.parseInt(s[3]);
            int t = Integer.parseInt(s[4]);
            double cc_pro = 0.0;
            double ss_pro = 0.0;
            for(int i = 1 ; i <= n ; i++){
             String[] pro = scan.nextLine().split(" ");
                for(int j = 1 ; j <= m ; j++){
                    double pro_t = Double.parseDouble(pro[j - 1]);
                    ss_pro += pro_t;
                    if((i == x) && (j == y)){
                     cc_pro = pro_t;
                    }
                }
            }
            ss_pro /= (n * m);
            if(cc_pro > ss_pro){
             System.out.println("cc");
             System.out.println(String.format("%.2f", 1 - Math.pow(1 - cc_pro, t)));

//System.out.printf("%.2f\n" , 1 - Math.pow(1 - cc_pro, t));
            }else if(cc_pro == ss_pro){
             System.out.println("equal");
             System.out.println(String.format("%.2f", 1 - Math.pow(1 - cc_pro, t)));
            }else{
             System.out.println("ss");
             System.out.println(String.format("%.2f", 1 - Math.pow(1 - ss_pro, t)));
            }
        }
        scan.close();
    }
}



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