Play the Dice

Description
There is a dice with n sides, which are numbered from 1,2,...,n and have the equal possibility to show up when one rolls a dice. Each side has an integer ai on it. Now here is a game that you can roll this dice once, if the i-th side is up, you will get ai yuan. What's more, some sids of this dice are colored with a special different color. If you turn this side up, you will get once more chance to roll the dice. When you roll the dice for the second time, you still have the opportunity to win money and rolling chance. Now you need to calculate the expectations of money that we get after playing the game once.
Input
Input consists of multiple cases. Each case includes two lines. 
The first line is an integer n (2<=n<=200), following with n integers a i(0<=a i<200) 
The second line is an integer m (0<=m<=n), following with m integers b i(1<=b i<=n), which are the numbers of the special sides to get another more chance.
Output
Just a real number which is the expectations of the money one can get, rounded to exact two digits. If you can get unlimited money, print inf. 
Sample Input
6 1 2 3 4 5 6
0
4 0 0 0 0
1 3
Sample Output
3.50
0.00


题目大意:
给出一个n,表示有一个正n面形,然后给出每个面的得分。接着是m,表示丢到哪些面后可以获得再掷一次的机会,问说得分的期望。
解题思路:
由于是无限,我们可以看作是
只考虑第一次,获得的金币的平均值为sum/n.sum为所有色子的面的金币值相加。
对于运气好,摇中了可以再来一次,该轮就能获得m/n*(sum/n)
运气好,又再来一次,该轮能获得(m/n)^2*(sum/n)
无穷无尽的摇下去,一共能获得sum/n*(1+p + p^2+`````+p^k + ````),其中p = m/n
将式子化简,就能得到E = sum/(n-m)。所以当sum = 0时为0,n=m时为inf。其余就为sum/(n-m)。

不过这个题目需要特判,如果sum=0首先输出0.00,然后是n==m,输出inf。因为题目会卡你sum==0且n==m这组答案。
#include <iostream>
#include<cstdio>
using namespace std;

int main()
{
    float a;
    float sum;
    int n,m;
    while(~scanf("%d",&n))
    {
        sum=0;
        for(int i=0;i<n;i++)
        {
          scanf("%f",&a);
          sum+=a;
        }
        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            scanf("%f",&a);
        }
        if(sum==0)
            printf("0.00\n");
        else if(n==m)
            printf("inf\n");
        else
        {
            float b=sum/(n-m);
            printf("%.2f\n",b);
        }

    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据以下要求编写一个python程序1. Description Ship of Fools is a simple classic dice game. It is played with five standard 6-faced dice by two or more players. - The goal of the game is to gather a 6, a 5 and a 4 (ship, captain and crew) in the mentioned order. - The sum of the two remaining dice (cargo) is preferred as high as possible. The player with the highest cargo score wins the round. Example: - In the first round turn, if a player rolls 6 4 3 3 1 (note we five dice at the beginning), the player can bank the 6 (ship), but the rest needs to be re-rolled since there is no 5. - In the second round turn, if the player rolls 6 5 4 4 (four dice, since the 6 from last turn is banked), the player can bank the 5 (captain) and the 4 (crew). The player has three choices for the remaining 6 and 4. The player can bank both and score 10 points, or re-roll one or two of the dice and hope for a higher score. - In the second round turn, if the player instead rolled 4 4 3 1, all dice needs to be re-rolled since there is no 5.程序需要包含一下几个类.The division of responsibility between the different classes is as follows. - Die: Responsible for handling randomly generated integer values between 1 and 6. - DiceCup: Handles five objects (dice) of class Die. Has the ability to bank and release dice individually. Can also roll dice that are not banked. - ShipOfFoolsGame: Responsible for the game logic and has the ability to play a round of the game resulting in a score. Also has a property that tells what accumulated score results in a winning state, for example 21. - Player: Responsible for the score of the individual player. Has the ability, given a game logic, play a round of a game. The gained score is accumulated in the attribute score. - PlayRoom: Responsible for handling a number of players and a game. Every round the room lets each player play, and afterwards check if any player has reached the winning score.
最新发布
06-02

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值