打赏

笔试题算法系列(三)百度钓鱼比赛

时间限制:1秒

空间限制:32768K

ss请cc来家里钓鱼,鱼塘可划分为n*m的格子,每个格子有不同的概率钓上鱼,cc一直在坐标(x,y)的格子钓鱼,而ss每分钟随机钓一个格子。问t分钟后他们谁至少钓到一条鱼的概率大?为多少?
输入描述:
第一行五个整数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)
输出描述:
输出两行。第一行为概率大的人的名字(cc/ss/equal),第二行为这个概率(保留2位小数)
输入例子1:
2 2 1 1 1
0.2 0.1
0.1 0.4
输出例子1:
equal
0.20

分析: 平均概率公式:1 - (1-p)^ t
    随机概率:通过求解均值来代替概率  ssP/(n*m)

代码如下:
 1 import java.util.Scanner;
 2 public class Main{
 3     public static void main(String [] args){
 4         Scanner sc = new Scanner(System.in);
 5         String [] line;
 6         while(sc.hasNext()){
 7             line = sc.nextLine().split(" ");
 8             int n = Integer.parseInt(line[0]);
 9             int m = Integer.parseInt(line[1]);
10             int x = Integer.parseInt(line[2])-1;
11             int y = Integer.parseInt(line[3])-1;
12             int t = Integer.parseInt(line[4]);
13             double ccP = 0;
14             double ssP = 0;
15             for(int i=0; i<n; i++){
16                 line = sc.nextLine().split(" ");
17                 for(int j=0; j<m; j++){
18                     ssP += Double.parseDouble(line[j]);
19                     if(i==x && j==y){
20                         ccP = Double.parseDouble(line[j]);
21                     }
22                 }
23             }
24             ccP = 1- Math.pow(1-ccP,t);    
25             ssP = 1- Math.pow(1-ssP/(n*m),t);
26             if(ssP > ccP){
27                 System.out.println("ss");
28                 System.out.println(String.format("%.2f",ssP));
29             }else if(ssP < ccP){
30                 System.out.println("cc");
31                 System.out.println(String.format("%.2f",ccP));
32             }else{
33                 System.out.println("equal");
34                 System.out.println(String.format("%.2f",ssP));
35             }
36         }
37         sc.close();
38     }
39 }

 

posted @ 2019-04-01 17:24  海米傻傻  阅读(217)  评论(0编辑  收藏  举报