[编程题] 钓鱼比赛
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
#include <vector>
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace::std ;
int main() {
int n, m, x, y, t ;
while ( cin >> n >> m >> x >> y >> t ) {
vector<vector<float>> vec( n, vector<float>( m, 0.0 ) ) ;
for ( int i = 0; i < n; ++ i ) {
for ( int j = 0; j < m; ++ j ) {
cin >> vec[i][j] ;
}
}
float px = vec[x - 1][y - 1] ;
px = 1 - pow( ( 1 - px ), t ) ;
float py = 0.0 ;
for ( int i = 0; i < n; ++ i ) {
for ( int j = 0; j < m; ++ j ) {
py += vec[i][j] ;
}
}
py = py / m / n ;
py = 1 - pow( ( 1 - py ) , t ) ;
cout.setf( ios::fixed ) ;
if ( py == px ) {
cout << "equal" << endl ;
cout << setprecision(2) << px << endl ;
} else if ( px > py ) {
cout << "cc" << endl ;
cout << setprecision(2) << px << endl ;
} else {
cout << "ss" << endl ;
cout << setprecision(2) << py << endl ;
}
}
return 0 ;
}
第二次做:
#include <iostream>
#include <vector>
#include <math.h>
#include <iomanip>
using namespace::std ;
int main() {
int n, m, x, y, t ;
while ( cin >> n >> m >> x >> y >> t ) {
vector<vector<float>> vec( n, vector<float>( m, 0.0 ) ) ;
for ( int i = 0; i < n; ++ i ) {
for ( int j = 0; j < m; ++ j ) {
cin >> vec[i][j] ;
}
}
float px = vec[x - 1][y - 1] ;
px = 1 - pow( ( 1 - px ), (double)t ) ;
float py = 0.0 ;
for ( int i = 0; i < n; ++ i ) {
for ( int j = 0; j < m; ++ j ) {
py += vec[i][j] ;
}
}
py = py / m / n ;
py = 1 - pow( ( 1 - py ), (double)t ) ;
cout << setiosflags( ios::fixed ) ;
if ( px == py ) {
cout << "equal" << endl ;
cout << setprecision( 2 ) << px << endl ;
} else if ( px > py ) {
cout << "cc" << endl ;
cout << setprecision( 2 ) << px << endl ;
} else {
cout << "ss" << endl ;
cout << setprecision( 2 ) << py << endl ;
}
}
return 0 ;
}
第三次做:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace::std ;
int main() {
int n, m, x, y, t ;
while ( cin >> n >> m >> x >> y >> t ) {
vector<vector<float>> vec( n + 1, vector<float>( m + 1, 0 ) ) ;
for ( int i = 1; i <= n; ++ i ) {
for ( int j = 1; j <= m; ++ j ) {
cin >> vec[i][j] ;
}
}
float cc = vec[x][y] ;
cc = 1 - pow( ( 1 - cc ), (double)t ) ;
float ss = 0.0 ;
for ( int i = 1; i <= n; ++ i ) {
for ( int j = 1; j <= m; ++ j ) {
ss += vec[i][j] ;
}
}
ss = ss / m / n ;
ss = 1 - pow( ( 1 - ss ), (double)t ) ;
cout.setf( ios::fixed ) ;
if ( ss > cc ) {
cout << "ss" << endl << setprecision(2) << ss << endl ;
}
else if ( ss < cc ) {
cout << "cc" << endl << setprecision(2) << cc << endl ;
}
else {
cout << "equal" << endl << setprecision(2) << ss << endl ;
}
}
return 0 ;
}