POJ 1322 Chocolate

POJ 1322 Chocolate

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

Chocolate
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8245   Accepted: 2186   Special Judge

Description

In 2100, ACM chocolate will be one of the favorite foods in the world.
 

“Green, orange, brown, red…”, colorful sugar-coated shell maybe is the most attractive feature of ACM chocolate. How many colors have you ever seen? Nowadays, it’s said that the ACM chooses from a palette of twenty-four colors to paint their delicious candy bits.
 

One day, Sandy played a game on a big package of ACM chocolates which contains five colors (green, orange, brown, red and yellow). Each time he took one chocolate from the package and placed it on the table. If there were two chocolates of the same color on the table, he ate both of them. He found a quite interesting thing that in most of the time there were always 2 or 3 chocolates on the table.
 

Now, here comes the problem, if there are C colors of ACM chocolates in the package (colors are distributed evenly), after N chocolates are taken from the package, what’s the probability that there is exactly M chocolates on the table? Would you please write a program to figure it out?
 

Input

The input file for this problem contains several test cases, one per line.
 

For each case, there are three non-negative integers: C (C <= 100), N and M (N, M <= 1000000).
 

The input is terminated by a line containing a single zero.
 

Output

The output should be one real number per line, shows the probability for each case, round to three decimal places.

Sample Input

5 100 2

0

Sample Output

0.625 

Source

 

题意:C种颜色的巧克力在桶中,从里面依次拿出n个巧克力,颜色同样的吃掉,求最后剩下m个巧克力的概率

当n>1000 时候,考虑奇偶性取1000或1001就可以,由于非常大的时候概率会趋于稳定,至于奇数时取1001 偶数

时取1000有些不解

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#define N 1010
using namespace std;
double dp[N][110];
int main()
{
    int c,n,m;
    while(scanf("%d",&c)!=EOF)
    {
        if(c==0)
        {
            break;
        }
        scanf("%d %d",&n,&m);
        if(m>c||m>n||(n-m)%2)
        {
            printf("0.000\n");
            continue;
        }
        if(n>1000)
        {
            n = 1000+n%2;
        }
        memset(dp,0,sizeof(dp));
        dp[0][0] = 1;
        dp[1][1] = 1;
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=i&&j<=c;j++)
            {
                if(j-1>=0)
                {
                    dp[i][j] = dp[i-1][j-1]*(double)(c-j+1)/(double)c;
                }
                dp[i][j] += dp[i-1][j+1]*(double)(j+1)/(double)c;
            }
        }
        printf("%.3lf\n",dp[n][m]);
    }
    return 0;
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/118765.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • DB4O详细介绍

    DB4O详细介绍深入db4o深入db4o这是RickGrehan发表在TheServerSide上的一篇关于面向对象数据库–db4o的文章,较全面地介绍了db4o的关键特性,希望对大家认识db4o能有所帮助。(2007.12.07最后更新)db4o-针对对象的数据库-是一个完全的对象数据库;它以使对象在其生命周期中-无论是在数据库内或是在外-都保持着它们的本性这样一种方…

    2022年7月21日
  • asp.net中通过input file上传文件

    asp.net中通过input file上传文件前台: 后台:

    2022年7月17日
  • 我的世界全自动刷矿机_我的世界服务器刷物资

    我的世界全自动刷矿机_我的世界服务器刷物资我的世界游戏中玩家可以操作一个建筑工人通过各种方块的摆放和破坏,来建造一个自己的世界,其中矿石的作用在游戏中是非常重要的,本次带来的我的世界刷矿机MOD就可以帮助玩家刷出的石头的同时有一定的几率变为各种矿石,助您轻松获取矿石资源!MOD功能当刷石机刷出石头后,石头有一定几率变成钻石、青金石、黄金、铁、红石矿石。使用方法MOD适用于游戏版本v1.12.2,需要Forge14.23.5.2768安装…

    2022年9月30日
  • MATLAB神经网络工具箱函数各种图的解释

    MATLAB神经网络工具箱函数各种图的解释介绍了matlab神经网络工具箱函数的图解属性和绘图意义

    2022年6月20日
  • 数据库分区及分区优点

    数据库分区及分区优点此文从以下几个方面来整理关于分区表的概念及操作:        1.表空间及分区表的概念        2.表分区的具体作用        3.表分区的优缺点        4.表分区的几种类型及操作方法        5.对表分区的维护性操作. (1.)表空间及分区表的概念 表空间:   是一个或多个数据文件的集合,所有的数据对象都存放在指定的表空间中

    2022年6月5日
  • asp.net 中mvc return Content(“ok”) ;return View() return JSon()区别

    asp.net 中mvc return Content(“ok”) ;return View() return JSon()区别

    2022年3月4日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号