ss请cc来家里钓鱼,鱼塘可划分为n*m的格子,每个格子有不同的概率钓上鱼,cc一直在坐标(x,y)的格子钓鱼,而ss每分钟随机钓一个格子。问t分钟后他们谁至少钓到一条鱼的概率大?为多少?
输入有多组数据。对于每组数据,第一行五个整数接下来为一个的矩阵,每行
个一位小数,共
行,第
行第
个数代表坐标为
的格子钓到鱼的概率为
输出两行。第一行为概率大的人的名字(cc/ss/equal),第二行为这个概率(保留2位小数)
2 2 1 1 1 0.2 0.1 0.1 0.4 2 2 1 1 1 0.1 0.2 0.3 0.4
equal 0.20 ss 0.25
//运行通过
#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
int main()
{
int row,column,x,y,minute;
while (cin >> row >> column >> x >> y >> minute)
{
double ** pArray = new double* [row];
for (int i = 0; i < row; ++i)
pArray[i] = new double[column];
for (int i = 0; i < row;++i)
for (int j = 0; j < column; ++j)
cin >> pArray[i][j];
double pCC = pArray[x-1][y-1];
double pSS = 0.0;
for (int i = 0; i < row;++i)
for (int j = 0; j < column; ++j)
pSS += pArray[i][j];
pSS = pSS / row / column;
cout << setiosflags(ios::fixed) << setprecision(2);
if (pSS < pCC)
cout << "cc" << endl << 1 - pow(1 - pCC, minute) << endl;
else {
if (pCC < pSS)
cout << "ss" << endl << 1 - pow(1 - pSS, minute) << endl;
else
cout << "equal" << endl << 1 - pow(1 - pSS, minute) << endl;
}
for (int i = 0; i < row; ++i)
delete[]pArray[i];
delete[]pArray;
}
return 0;
}
while 1:
try:
s = raw_input()
except:
break
n, m, x, y, t = map(float, s.split())
s = 0.0
for i in range(int(n)):
d = map(float, raw_input().split())
s += sum(d)
if x - 1 == i:
p1 = d[int(y) - 1]
p2 = s / (n * m)
t = [('cc', 1 - (1 - p1) ** t), ('ss', 1 - (1 - p2) ** t)]
t.sort(key=lambda d: d[1])
print 'equal' if t[0][1] == t[1][1] else t[-1][0]
print '%.2f' % t[-1][1]
#include<iostream>
#include<vector>
#include<cmath>
#include <iomanip>
using namespace std;
int main(){
int n,m,x,y,t;
while(cin>>n>>m>>x>>y>>t){
double sum=0;
vector<vector<double>> p(n,vector<double>(m,0.0));
for(int i=0;i<n;++i){
for(int j=0;j<m;++j){
cin>>p[i][j];
sum+=p[i][j];
}
}
double pcc,pss;
pcc=1-pow(1-p[x-1][y-1],t);
pss=1-pow(1-sum/(n*m),t);
if(pcc>pss){
cout<<"cc"<<endl<<setiosflags(ios::fixed)<< setprecision(2)<<pcc<<endl;
}
else if(pcc<pss){
cout<<"ss"<<endl<<setiosflags(ios::fixed)<< setprecision(2)<<pss<<endl;
}
else
cout<<"equal"<<endl<<setiosflags(ios::fixed)<< setprecision(2)<<pss<<endl;
}
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <iomanip>
using namespace std;
int main()
{
int n, m, x, y, t;
while (cin >> n>>m>>x>>y>>t)
{
vector<vector<double>> f(n + 1, vector<double>(m + 1, 0.0));
double sum = 0.0;
for (size_t i = 1; i < n+1; i++)
{
for (int j = 1; j < m+1; j++)
{
cin >> f[i][j];
sum += f[i][j];
}
}
double cc = 1 - pow(1.0-f[x][y], t);
double ss = sum /(m*n);
ss = 1 - pow(1.0 - ss, t);
if (cc==ss)
{
cout << "equal" << endl<<setiosflags(ios::fixed)<< setprecision(2) << ss << endl;
}
else if(cc>ss)
{
cout << "cc" <<endl <<setiosflags(ios::fixed)<< setprecision(2) << cc << endl;
}
else
{
cout << "ss" << endl <<setiosflags(ios::fixed)<< setprecision(2) << ss << endl;
}
}
return 0;
}
// 求纠错,牛客的OJ系统,还需要学习啊
import java.text.DecimalFormat;
import java.io.*;
import java.util.*;
public class Main {
public static Double[] solution3(double[][] a, int n, int m, int x, int y, int t){
double res = 0;
double cc = 1-Math.pow(1-a[x][y], t); // 1-(1-P)^t
double ss_r = 1;
for(int i=0;i<t;i++){
int x0 = (int)Math.random()*n;
int y0 = (int)Math.random()*m;
ss_r *= 1-a[x0][y0]; // (1-P)^t
}
double ss = 1 - ss_r; // 1-(1-P)^t
return new Double[]{cc, ss};
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int m = cin.nextInt();
int x = cin.nextInt()-1;
int y = cin.nextInt()-1;
int t = cin.nextInt();
double[][] a = new double[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
a[i][j] = cin.nextDouble();
}
}
Double[] res = solution3(a, n, m, x, y, t);
if(res[0]-res[1]>=0.01){
System.out.println("cc");
System.out.println(new DecimalFormat("0.00").format(res[0]));
}
else if(res[0]-res[1]<=-0.01){
System.out.println("ss");
System.out.println(new DecimalFormat("0.00").format(res[1]));
}
else{
System.out.println("equal");
System.out.println(new DecimalFormat("0.00").format(res[0]));
}
}
}
// 一个概率计算问题,但是里面有很多细节问题需要注意import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = 0; int m = 0; int x = 0; int y = 0; int t = 0; double temp = 0.0; double p_cc = 0.0; double p_ss = 0.0; while(sc.hasNext()){ n = sc.nextInt(); m = sc.nextInt(); x = sc.nextInt(); y = sc.nextInt(); t = sc.nextInt(); sc.nextLine(); temp = 0.0; p_cc = 0.0; p_ss = 0.0; for(int i=1;i<=n;i++){ String[] conten = sc.nextLine().split(" "); for(int j=1;j<=m;j++){ temp = Double.parseDouble(conten[j-1]); p_ss += temp; if(i==x && j==y)p_cc = temp; } } p_ss = p_ss/(m*n); if(p_ss>p_cc){ System.out.println("ss"); System.out.println(String.format("%.2f", 1.0-Math.pow(1.0-p_ss,t))); }else if(p_ss<p_cc){ System.out.println("cc"); System.out.println(String.format("%.2f", 1.0-Math.pow(1.0-p_cc,t))); }else{ System.out.println("equal"); System.out.println(String.format("%.2f", 1.0-Math.pow(1.0-p_cc,t))); } } } }
#include<iostream>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<vector>
using namespace std;
int main()
{
int n,m,x,y,t;
while(cin>>n>>m>>x>>y>>t)
{
vector<vector<double> > a(n,vector<double>(m,0.0));
double sumProb=0;
for(int i=0;i<n;++i)
{
for(int j=0;j<m;++j)
{
cin>>a[i][j];
sumProb+=a[i][j];
}
}
double pcc=1-pow(1-a[x-1][y-1],t);
double pss=1-pow(1-sumProb/n/m,t); //sumProb/n/m为平均概率,其实就是随机概率
if(pcc>pss)
{ //首先,需要包含头文件#include<iomanip>,该头文件控制输入输出流的格式,
cout<<"cc"<<endl; //其次:setiosflags(iOS::fixed)与后面的setprecision(1)连用可以控制输出小数小数点后面的位数。
cout<<setiosflags(ios::fixed)<<setprecision(2)<<pcc<<endl;
}
else if(pcc<pss)
{
cout<<"ss"<<endl;
cout<<setiosflags(ios::fixed)<<setprecision(2)<<pss<<endl;
}
else
{
cout<<"equal"<<endl;
cout<<setiosflags(ios::fixed)<<setprecision(2)<<pss<<endl;
}
}
return 0;
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()){
int n=in.nextInt();
int m=in.nextInt();
int x=in.nextInt();
int y=in.nextInt();
int t=in.nextInt();
in.nextLine();//吸取换行符
int len=n*m;
double ss=0,cc=0;
double arr[][]=new double[n][m];
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < m; j++) {
// double d=in.nextDouble();
// arr[i][j]=d;
// ss+=d;
// }
// }
for (int i = 0; i < n; i++) {
//将第一行读到的数字按照空格分开存放在字符串数组中,这个数组的大小就是m
String[] content = in.nextLine().split(" ");
for (int j = 0; j < m; j++) {
//然后再把没一个字符串强转放到二维数组里面就好了
double d=Double.parseDouble(content[j]);
arr[i][j] = d;
ss+=d;
}
}
cc=arr[x-1][y-1];
ss/=len;
double rss=1-Math.pow(1-ss, t);
double rcc=1-Math.pow(1-cc, t);
if(rss>rcc){
System.out.println("ss");
System.out.format("%.2f\n",rss);
}
else if(rss==rcc){
System.out.println("equal");
System.out.format("%.2f\n",rss);
}
else{
System.out.println("cc");
System.out.format("%.2f\n",rcc);
}
}
}
}
这样写为啥不对,求解答
#include<cstdio>
#include<cmath>
float arr[1000+1][1000+1]={0};
int main(){
int rows,cols;
int x,y;
int t;
scanf("%d %d %d %d %d",&rows,&cols,&x,&y,&t);
double statc_b=0;
for(int i=1;i<=rows;i++){
for(int j=1;j<=cols;j++){
scanf("%f",&arr[i][j]);
statc_b+=arr[i][j];
}
}
double stac_a=arr[x][y];
double all_stac_a=pow(1-stac_a,t);
double statc_b_2=statc_b/(rows*cols);
double all_stac_b=pow(1-statc_b_2,t);
if(fabs(all_stac_a-all_stac_b)<0.0001){
printf("equal\n");
printf("%.2f\n",1-all_stac_a);
}else if(all_stac_a<all_stac_b){
printf("cc\n");
printf("%.2f\n",1-all_stac_a);
}else{
printf("ss\n");
printf("%.2f",1-all_stac_a);
}
}
//每次钓到鱼的概率为p,t分钟后钓到一条鱼概率为1-(1-p)^t
#include<stdio.h>
#include<iostream>
#include<vector>
#include<math.h>
using namespace std;
int main(){
int n,m,x,y,t;
while(cin>>n>>m>>x>>y>>t){
vector<vector<float>> matrix(n,vector<float>(m,0));
float CCpro = 0;
float SSpro = 0;
for(int i=0; i<n; ++i){
for(int j=0; j<m; ++j){
cin >> matrix[i][j];
if(i==x-1&&j==y-1)
CCpro = matrix[i][j];
SSpro += matrix[i][j];
}
}
SSpro = SSpro/(m*n);
double ret = 0;
if(SSpro == CCpro){
cout<<"equal"<<endl;
ret = 1- pow(1-SSpro,t);
printf("%.2f\n",ret);
}
if(SSpro > CCpro){
cout<<"ss"<<endl;
ret = 1- pow(1-SSpro,t);
printf("%.2f\n",ret);
}
if(SSpro < CCpro){
cout<<"cc"<<endl;
ret = 1- pow(1-CCpro,t);
printf("%.2f\n",ret);
}
}
return 0;
}
//以示例为例,对于cc而言,每次钓到鱼的概率p=0.2,t分钟后至少钓到一条鱼的概率pcc=1-(1-0.2)^t,
//对于ss,有n*m=4个位置供选择,故选择每个位置的概率为1/4,故每次钓到鱼的概率为p=1/4*0.2+1/4*0.1+1/4*0.1+1/4*0.4,
//故t分钟后至少钓到一条鱼的概率为pss=1-(1-p)^t。最后比较pcc和pss并输出。
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double prob[1005][1005];
int main(){
int n, m, x, y, t;
while(cin >> n >> m >> x >> y >> t){
int i, j;
double sum = 0.0;
for(i = 1; i <= n; ++i)
for(j = 1; j <= m; ++j){
cin >> prob[i][j];
sum += prob[i][j];
}
double pcc = 1.0 - pow((1 - prob[x][y]), t);
double pss = sum / (n*m);//钓到鱼的随机概率
pss = 1.0 - pow((1 - pss), t);
if(pss == pcc)
cout << "euqal\n" << fixed << setprecision(2) << pcc << endl;
else if(pss > pcc)
cout << "ss\n" << fixed << setprecision(2) << pss << endl;
else
cout << "cc\n" << fixed << setprecision(2) << pcc << endl;
}
return 0;
}
import java.util.Scanner;
public class Main {
/**
* 非要逐行读取才不超时!
*/
//思路:cc:固定某点概率;ss:所有点求平均概率,再由独立事件公式求t分钟后
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
while(reader.hasNext()){
String[] s1 = reader.nextLine().split(" ");
int n = Integer.parseInt(s1[0]);
int m = Integer.parseInt(s1[1]);
int x = Integer.parseInt(s1[2]);
int y = Integer.parseInt(s1[3]);
int t = Integer.parseInt(s1[4]);
double proCC = 0;
double sumPro = 0;
for(int i=1; i<=n; i++){
String[] s = reader.nextLine().split(" ");
for(int j=1; j<=m; j++){
double p = Double.parseDouble(s[j-1]);
sumPro += p;
if((i == x) && (j == y)){
proCC = p;
}
}
}
sumPro /= (n*m);
//!注意这里:t个独立事件:P(t1Ut2Ut3..Utn) = 1-P(非t1)P(非t2)...P(非t3)
if(proCC == sumPro){
System.out.println("equal");
System.out.println(String.format("%.2f", 1 - Math.pow(1-proCC, t))); //保留小数点后两位
}else if(proCC > sumPro){
System.out.println("cc");
System.out.println(String.format("%.2f", 1 - Math.pow(1-proCC, t)));
}else if(proCC < sumPro){
System.out.println("ss");
System.out.println(String.format("%.2f", 1 - Math.pow(1-sumPro, t)));
}
}
}
}
import java.util.Scanner;
//题目并不难,已通过,
//简单的说一下吧,首先我也不知道他这个输入时按照行来输入的,
//所以看了牛友的讨论才知道,输入是按照行扫描的,表示又涨姿势了
//另外就是注意题目描述,之少钓到一条鱼的概率,不要忽略了之少这个关键字。
//所以就可以考虑对立事件了。一条鱼也钓不到的概率。然后用1减去对立事件概率就解决了
public class Main{
public static void fish(double[][] rectangle, double cc, int t) {
//count记录所有格子的累加概率之和。因为随机钓一个格子,所以钓每个格子的概率
//都是一样的,都是等概率。
double ss = 0;
for (int i = 0; i < rectangle.length; i++) {
for (int j = 0; j < rectangle[0].length; j++) {
ss += rectangle[i][j];
}
}
//用累加的概率之和除以格子的总数,就是ss在每个格子钓到鱼的平均概率。
//也就相当于他固定在一个格子上进行钓鱼。这个格子的概率也是固定的。
ss = ss / (rectangle.length * rectangle[0].length);
if (ss > cc) {
System.out.println("ss");
//因为至少调到一条鱼的情况比较多,所以考虑对立事件,一个鱼也钓不到的情况
//又因为每分钟钓到鱼和钓不到鱼都是独立事件,互不影响,(感觉又回到高中了 = =)
//把每分钟钓不到鱼的概率相乘,也就是求它的t次方,就是t分钟钓不到鱼的概率了。
//最后用1减去这个概率就是能钓到鱼的概率了。
//下面的分析同理。
System.out.println(String.format("%.2f", 1 - Math.pow(1 - ss, t)));
} else if (cc > ss) {
System.out.println("cc");
System.out.println(String.format("%.2f", 1 - Math.pow(1 - cc, t)));
} else {
System.out.println("equal");
System.out.println(String.format("%.2f", 1 - Math.pow(1 - cc, t)));
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
do {
int n = sc.nextInt();
int m = sc.nextInt();
int x = sc.nextInt() - 1;
int y = sc.nextInt() - 1;
int t = sc.nextInt();
//注意一定要换行
sc.nextLine();
double[][] rectangle = new double[n][m];
for (int i = 0; i < n; i++) {
//将第一行读到的数字按照空格分开存放在字符串数组中,这个数组的大小就是m
String[] conten = sc.nextLine().split(" ");
for (int j = 0; j < m; j++) {
//然后再把没一个字符串强转放到二维数组里面就好了
rectangle[i][j] = Double.parseDouble(conten[j]);
}
}
fish(rectangle, rectangle[x][y], t);
} while (sc.hasNext());
}
}
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while (in.hasNext()) {//注意while处理多个case
String[] s1 = in.nextLine().split(" ");
int n = Integer.parseInt(s1[0]);
int m = Integer.parseInt(s1[1]);
int x = Integer.parseInt(s1[2]);
int y = Integer.parseInt(s1[3]);
int t = Integer.parseInt(s1[4]);
//int n = in.nextInt();
//int m = in.nextInt();
//int x = in.nextInt();
//int y = in.nextInt();
//int t = in.nextInt();
double ccp = 0.00;
double ssp = 0.00;
for(int i = 1;i<=n;i++){
String[] s = in.nextLine().split(" ");
for(int j = 1;j<=m;j++){
double p = 1-Double.parseDouble(s[j-1]);
//double p = 1-in.nextDouble();//钓不到鱼的概率
if(i==x&&j==y)
ccp = p;
ssp += p;
}
}
ssp /= (n*m);//期望
if(ccp<ssp){
System.out.println("cc");
System.out.printf("%.2f\n", 1-Math.pow(ccp,t));
}else if(ccp>ssp){
System.out.println("ss");
System.out.printf("%.2f\n", 1-Math.pow(ssp,t));
}else{
System.out.println("equal");
System.out.printf("%.2f\n", 1-Math.pow(ccp,t));
}
}
}
}
#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
int main()
{
int n,m,x,y,t;
float cc,ss,equal;
while(cin>>n>>m>>x>>y>>t)
{
float **p;
p=new float *[n];
for(int i=0;i<n;i++)
p[i]=new float[m];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
cin>>p[i][j];
}
cc=1-pow((1-p[x-1][y-1]),t);
ss=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
ss+=p[i][j];
ss=ss/m/n;
ss=1-pow(1-ss,t);
cout.setf(ios::fixed);
if(ss==cc)
{
equal=ss;
cout<<"equal"<<endl;
cout<<setprecision(2)<<equal<<endl;
}
if(ss<cc)
{
cout<<"cc"<<endl;
cout<<setprecision(2)<<cc<<endl;
}
if(ss>cc)
{
cout<<"ss"<<endl;
cout<<setprecision(2)<<ss<<endl;
}
}
return 0;
}
t时间内至少钓到一条的概率等于1-(1-P)^t 其中P为每一次钓到的概率。对于cc来说就是Pxy 对于ss来说就是所有概率求和再除以(m*n)即可
注意浮点数不能比较相等 需要比较差小于一个很小的数
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int main(){
int n, m, x, y, t;
while(scanf("%d%d%d%d%d", &n, &m, &x, &y, &t) == 5){
double sum = 0, pbase, tmp;
for(int i=1; i <= n; i++)
for(int j=1; j <= m; j++){
scanf("%lf", &tmp);
if(i == x && j == y) pbase = tmp;
sum += tmp;
}
double pcc = 1 - pow(1-pbase, t);
double pss = 1 - pow(1-sum/(m*n), t);
if(pcc > pss) printf("cc\n%.2lf\n", pcc);
else if(pcc <pss) printf("ss\n%.2lf\n", pss);
else if(abs(pcc-pss) < 1e-4) printf("equal\n%.2lf\n", pcc);
}
return 0;
}
while 1:
try:
n, m, x, y, t = map(int, input().strip().split())
grid = []
for _ in range(n):
grid.append(list(map(float, input().strip().split())))
# cc在t分钟后一条鱼都钓不到的概率
p1 = (1 - grid[x - 1][y - 1])**t
# ss在t分钟后一条鱼都钓不到的概率
p2 = 0
for i in range(n):
for j in range(m):
p2 += 1 - grid[i][j]
p2 = p2**t/((n*m)**t)
# 根据概率大小输出结果
if p1 < p2:
print("cc")
print("%.2f" % (1 - p1))
elif p1 > p2:
print("ss")
print("%.2f" % (1 - p2))
else:
print("equal")
print("%.2f" % (1 - p1))
except:
break #include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main(void)
{
int n, m, x, y, t;
while (cin >> n >> m >> x >> y >> t){
double nub, pobsum = 0, pobc = 0, pobs = 0;
double *pobcc = new double[t], *pobss = new double[t];
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> nub;
pobsum += nub; //求各点概率和,以求ss概率
if (i == x && j == y) pobc = nub; //记录cc概率
}
}
pobs = pobsum / (n * m);
pobcc[0] = pobc, pobss[0] = pobs;
for (int i = 1; i < t; i++)
{
pobcc[i] = pow(1 - pobc, i)*pobc; //计算各次概率
pobss[i] = pow(1 - pobs, i)*pobs;
}
pobc = 0, pobs = 0;
for (int i = 0; i < t; i++)
{
pobc += pobcc[i]; //各次概率相加
pobs += pobss[i];
}
cout.setf(ios::fixed);
if (pobc > pobs) cout << "cc\n" << setprecision(2) << pobc << endl;
else if (pobc < pobs) cout << "ss\n" << setprecision(2) << pobs << endl;
else cout << "equal\n" << setprecision(2) << pobc << endl;
delete []pobss, delete []pobcc;
}
return 0;
}
求纠错,估计涉及到随机函数就不可能与测试案例一直通过
int main(){
int n,m,x,y,t;
cin>>n>>m>>x>>y>>t;
double A[n][m];
double ss,cc=0;
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
cin>>A[i][j];
}
}
int col,row;
for(int k = 0;k<t;k++){
ss = ss*(1-A[x-1][y-1]);
srand((unsigned)time(NULL));
col = rand()%n;
srand((unsigned)time(NULL));
row = rand()%m;
cc=cc*(1-A[col][row]);
}
ss=1-ss;
cc=1-cc;
double r = ss>cc?ss:cc;
char *s="ss";
char *c="cc";
char* e = "equal";
string res = ss>cc?s:(ss==cc?e:c);
cout<<res<<endl;
cout<<r;