解题思路

需要计算:

  1. cc在固定位置钓鱼 分钟的概率
  2. ss随机钓鱼 分钟的概率
  3. 比较两者的概率并输出结果

关键点

  1. 使用while循环处理多组输入
  2. 计算至少钓到一条鱼的概率
  3. 处理浮点数比较和输出格式

代码

#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
using namespace std;

class Solution {
public:
    pair<string, double> solve(int n, int m, int x, int y, int t, 
                             vector<vector<double>>& probs) {
        // cc在固定位置钓鱼的概率
        double cc_prob = 1 - pow(1 - probs[x-1][y-1], t);
        
        // ss随机钓鱼的平均概率
        double sum = 0;
        for (const auto& row : probs) {
            for (double p : row) {
                sum += p;
            }
        }
        double avg_prob = sum / (n * m);
        double ss_prob = 1 - pow(1 - avg_prob, t);
        
        // 比较概率
        const double EPS = 1e-10;
        if (abs(cc_prob - ss_prob) < EPS) {
            return {"equal", cc_prob};
        } else if (cc_prob > ss_prob) {
            return {"cc", cc_prob};
        } else {
            return {"ss", ss_prob};
        }
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n, m, x, y, t;
    while (cin >> n >> m >> x >> y >> t) {
        vector<vector<double>> probs(n, vector<double>(m));
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                cin >> probs[i][j];
            }
        }
        
        Solution solution;
        auto [winner, prob] = solution.solve(n, m, x, y, t, probs);
        
        cout << winner << endl;
        cout << fixed << setprecision(2) << prob << endl;
    }
    
    return 0;
}
import java.util.*;

public class Main {
    static class Solution {
        public static class Result {
            String winner;
            double probability;
            
            Result(String w, double p) {
                winner = w;
                probability = p;
            }
        }
        
        public Result solve(int n, int m, int x, int y, int t, 
                          double[][] probs) {
            // cc在固定位置钓鱼的概率
            double cc_prob = 1 - Math.pow(1 - probs[x-1][y-1], t);
            
            // ss随机钓鱼的平均概率
            double sum = 0;
            for (double[] row : probs) {
                for (double p : row) {
                    sum += p;
                }
            }
            double avg_prob = sum / (n * m);
            double ss_prob = 1 - Math.pow(1 - avg_prob, t);
            
            // 比较概率
            final double EPS = 1e-10;
            if (Math.abs(cc_prob - ss_prob) < EPS) {
                return new Result("equal", cc_prob);
            } else if (cc_prob > ss_prob) {
                return new Result("cc", cc_prob);
            } else {
                return new Result("ss", ss_prob);
            }
        }
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            int m = sc.nextInt();
            int x = sc.nextInt();
            int y = sc.nextInt();
            int t = sc.nextInt();
            
            double[][] probs = new double[n][m];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    probs[i][j] = sc.nextDouble();
                }
            }
            
            Solution solution = new Solution();
            Solution.Result result = solution.solve(n, m, x, y, t, probs);
            
            System.out.println(result.winner);
            System.out.printf("%.2f%n", result.probability);
        }
        sc.close();
    }
}
def solve(n: int, m: int, x: int, y: int, t: int, probs: list) -> tuple:
    # cc在固定位置钓鱼的概率
    cc_prob = 1 - (1 - probs[x-1][y-1]) ** t
    
    # ss随机钓鱼的平均概率
    avg_prob = sum(sum(row) for row in probs) / (n * m)
    ss_prob = 1 - (1 - avg_prob) ** t
    
    # 比较概率
    if abs(cc_prob - ss_prob) < 1e-10:
        return "equal", cc_prob
    elif cc_prob > ss_prob:
        return "cc", cc_prob
    else:
        return "ss", ss_prob

def main():
    while True:
        try:
            # 读取输入
            n, m, x, y, t = map(int, input().split())
            probs = []
            for _ in range(n):
                row = list(map(float, input().split()))
                probs.append(row)
            
            # 计算结果
            winner, prob = solve(n, m, x, y, t, probs)
            
            # 输出结果
            print(winner)
            print(f"{prob:.2f}")
            
        except EOFError:
            break

if __name__ == "__main__":
    main()

算法及复杂度

  • 算法:概率计算
  • 时间复杂度:,需要遍历概率矩阵计算平均概率
  • 空间复杂度:,需要存储概率矩阵