[编程题] 钓鱼比赛

[编程题] 钓鱼比赛
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位小数)

输入例子:
2 2 1 1 1
0.2 0.1
0.1 0.4

输出例子:
equal
0.20


头文件:iomanip
(1)setprecision(n),精确度为n,包括小数点前的数;
(2)设置小数点后的位数:先cout.setf(ios::fixed),在 cout << setprecision(2) << t << endl;(小数点后2位)
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <limits.h>
  5. #include <iomanip>
  6. using namespace std;
  7. int main(){
  8. int n,m,x,y,t;
  9. while(cin >> n >> m >> x >> y >> t){
  10. vector<vector<double>> vec(n, vector<double>(m,0));
  11. double sum = 0;
  12. for(int i = 0; i < n; ++i){
  13. for(int j = 0; j < m; ++j){
  14. cin >> vec[i][j];
  15. sum += vec[i][j];
  16. }
  17. }
  18. double cc = vec[x-1][y-1];
  19. double ccp = 1-pow(1-cc,t);
  20. double ss = sum/n/m;
  21. double ssp = 1-pow(1-ss,t);
  22. cout.setf(ios::fixed);
  23. if(ccp == ssp){
  24. cout << "equal" << endl;
  25. printf("%.2f ", ccp);
  26. //cout << setprecision(2) << ccp << endl;
  27. }
  28. else if(ccp > ssp){
  29. cout << "cc" << endl;
  30. printf("%.2f ", ccp);
  31. //cout << setprecision(2) << ccp << endl;
  32. }
  33. else{
  34. cout << "ss" << endl;
  35. printf("%.2f ", ssp);
  36. //cout << setprecision(2) << ssp << endl;
  37. }
  38. }
  39. }





原文地址:https://www.cnblogs.com/pukaifei/p/5738451.html