首页 > 试题广场 >

10个房间里放着随机数量的金币。每个房间只能进入一次,并只能

[问答题]
10个房间里放着随机数量的金币。每个房间只能进入一次,并只能在一个房间中拿金币。 一个人采取如下策略:前四个房间只看不拿。随后的房间只要看到比前四个房间都多的金币数, 就拿。否则就拿最后一个房间的金币。编程计算这种策略拿到最多金币的概率。
推荐
#include<iostream>
#include<math.h>
#include <time.h>
using namespace std;
int num[10];
inline void SrandNum()
{
    num[0]=rand()%100000;
    num[1]=rand()%100000;
    num[2]=rand()%100000;
    num[3]=rand()%100000;
    num[4]=rand()%100000;
    num[5]=rand()%100000;
    num[6]=rand()%100000;
    num[7]=rand()%100000;
    num[8]=rand()%100000;
    num[9]=rand()%100000;
}
int MaxNum()
{
    int Max=-1;
    for(int i=0;i<10;i++)
    {
        if(num[i]>=Max)
            Max=num[i];
    }
    return Max;
}
int AfterMax()
{
    int Max=-1;
    for(int i=0;i<4;i++)
        if(num[i]>=Max)
            Max=num[i];
        for(int i=4;i<9;i++)
            if(num[i]>Max)
                return num[i];
    return num[9];
}
int main()
{
    srand((unsigned) time(NULL));
    int times=0;
    int total=0;
    int max=-1;
    int afternum=-1;
    while(times<1000000)
    {
        SrandNum();
        max=MaxNum();
        afternum=AfterMax();
        if(afternum==max)
            total++;
        times++;
    }
    cout<<total<<endl;
    return 0;
}
数值维持在39.8%左右。
编辑于 2015-02-05 09:48:48 回复(2)
xxj头像 xxj
假设10个房间的金币数分别为C:{1,2,3,4,5,6,7,8,9,10}

设置拿到最大金币数为yes=0,没拿到为no=0,查找总次数为num
程序大概这样:

程序进入:打乱C序列
然后遍历前4个:找出最大的一个值max,
然后从第五开始遍历直到地9个:
如果找打比max大的这个数,ok停止再找,判断这个数是否是10,如果是,yes=yes+1
如果不是:no=no+1
如果任然没找到:判断第10个数是否是10,如果是yes =yes+1,否则no=no+1


这样重复多次后:计算概率为:yes/(yes+no)

发表于 2014-11-20 16:01:01 回复(2)
10!种可能性
发表于 2014-10-11 22:49:38 回复(1)
hcj头像 hcj
要注意最多金币数出现在每一个房间的概率都是相等的。
 

按这种策略,要拿到最多的金币,只有以下6种情况:
    
情况A:第5个房间的金币数最多,也就是第1~10个房间中的最大金币数出现在第5个房间。这种情况出现的概率是1/10。

 
情况B:第6个房间的金币数最多、且第5个房间的金币数不大于第1~4个房间金币数的最大值。也就是第1~10个房间中的最大金币数出现在第6个房间、且第1~5个房间中最大金币数出现在第1~4个房间。这种情况出现的概率是1/10*4/5。
 
情况C:第1~10个房间中的最大金币数出现在第7个房间、且第1~6个房间中最大金币数出现在第1~4个房间。概率是1/10*4/6。
 
情况D:第1~10个房间中的最大金币数出现在第8个房间、且第1~7个房间中最大金币数出现在第1~4个房间。概率是1/10*4/7。
 
情况E:第1~10个房间中的最大金币数出现在第9个房间、且第1~8个房间中最大金币数出现在第1~4个房间。概率是1/10*4/8。
 
情况F:第1~10个房间中的最大金币数出现在第10个房间、且第1~9个房间中最大金币数出现在第1~4个房间。概率是1/10*4/9。
 
因此总的概率是:1/10*(1+4/5+4/6+4/7+4/8+4/9)=0.3983。
编辑于 2015-07-17 00:59:17 回复(1)

package wangyi2013;
import java.util.*;
public class solution6test {
 public static void main(String[] args)
 {
  double pr;
  int[] rooms=new int[10];
  Random r = new Random();
  int i,j;
  int max=0;
  int maxOfFour=0;
  int total=1000000;
  int choseMax=0;
  for(i=0 ; i<total ;  i++)
   {
   for(j=0;j<rooms.length;j++)
   {
    rooms[j]=r.nextInt(100000000);
   }
   int chose=-1;
   for(j=0;j<rooms.length;j++)
   {
    if(j<=3&&rooms[maxOfFour]<rooms[j])
     maxOfFour=j;
    if(rooms[max]<rooms[j])
     max=j;
    if(j>3&&j<rooms.length-1)
    {
     if(rooms[j]>rooms[maxOfFour]&&chose==-1)     
      chose=j;
    }
    else if(j==rooms.length-1&&chose==-1)
    {
     chose=rooms.length-1;
    }
   }
   if(chose==max)
   {
    choseMax+=1;
   }
   }
  pr=(double)choseMax/(double)total;
  System.out.println(pr);
 }
发表于 2020-06-09 23:42:08 回复(0)
#include<iostream>
#include<string>
#include<algorithm>
#include<time.h>
#include<vector>
using namespace std;
int main(){
    // 随机数
    vector<int>  goldVec;
    srand((unsigned)time(NULL));
    int elem=0;
    for (int i = 0; i<10; i++){
        elem=rand()%100;
        goldVec.push_back(elem);
    }
    //标记金子最大的数目
    int max_num=goldVec[0];
    for (int i = 0; i<4; i++){
        if (max_num < goldVec[i]){
            max_num = goldVec[i];
        }
    }
    int j=4;
    int get_gold=0;
    for (; j < 10; j++){
        if (goldVec[j]>max_num){
            get_gold = goldVec[j];
             break;
        }      
    }
    if (j==10)
    {
        get_gold = goldVec[9];
    }

    cout<<get_gold<<endl;
    system("pause");
    return 0;
}
发表于 2016-06-24 11:39:59 回复(0)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <vector>
using namespace std;

int getPercent()
{
	vector<int> vec;
	int maxFour = 0;
	int maxTotal = 0;
	int result = 0;
	for (int i = 0; i < 10; ++i){
		vec.push_back(rand()%1000000);
		if (maxTotal < vec[i])
			maxTotal = vec[i];
	}
	for (int i = 0; i < 4; ++i){
		if (vec[i] > maxFour)
			maxFour = vec[i];
	}
	for (int i = 4; i < 10; ++i){
		if (vec[i] > maxFour || i == 9){
			result = vec[i];
			break;
		}
	}
	if (result < maxTotal)
		return -1;
	else return 1;

}
int main()
{
	srand((unsigned)time(NULL));
	int count = 0;
	for (int i = 0; i < 100000; ++i){
		if (getPercent() == 1){
			count++;
		}
	}
	cout << "The percentage is " << setprecision(2) << count / 100000.00;
	system("pause");
}

编辑于 2016-03-22 14:39:03 回复(0)
import java.util.Random;

public class CodeDemo01 {

	public static void main(String[] args) {
		int total=0;;
		CodeDemo01 cd=new CodeDemo01();
		for(int i=0;i<1000000000;i++){
			int[] arr=cd.GetRandomSequence(10);
			if(findOut(arr)) total++;
		}
		System.out.println(total);
	}

	public static boolean findOut(int[] arr){
		int max=0;
		int k;
		for(int i =0;i<4;i++){
			max=max>arr[i]?max:arr[i];
		}
		for(int i= 4;i<10;i++){
			if(arr[i]>max){
				if(arr[i]==9)return true;
				return false;
			}
		}
		return false;
	}
		
	public static int[] GetRandomSequence(int total) {

		int[] sequence = new int[total];
		int[] output = new int[total];

		for (int i = 0; i < total; i++) {
			sequence[i] = i;
		}
		Random random = new Random();
		int end = total - 1;

		for (int i = 0; i < total; i++) {
			int num = random.nextInt(end + 1);
			output[i] = sequence[num];
			sequence[num] = sequence[end];
			end--;
		}
		return output;
	}
}


发表于 2016-03-19 18:09:22 回复(0)
#include<iostream>
#include<vector>
#include<time.h>
#include<stdlib.h>
using namespace std;

int main()
{

	vector<int> icon;
	srand((unsigned)time(NULL));
	
	for (int I = 0; I < 5; I++)
	{
		float yes = 0;
		
		for (int j = 0; j < 100; j++)
		{
			int max = 0;
			icon.clear();
			for (int i = 0; i < 10; i++)
			{
				int n = rand() % 9999+1;
				icon.push_back(n);
			}

			for (int i = 0; i < 4; i++)
			{
				if (icon[i]>max)
					max = icon[i];
			}
			int flag = 0;
			for (int i = 4; i < 10; i++)
			{
				if (icon[i]>=max)
				{
					flag = 1;
					break;
				}
			}
			if (flag)
				yes++;
		}
		cout << yes  << endl;;
	}
	
	system("pause");
	return 0;
}

发表于 2016-03-01 21:47:56 回复(0)