projecteuler No.121 Disc game prize fund

题目链接:

Problem 121 Disc game prize fund

题目121:考察彩色圆盘的游戏。

通过人数:4953


题目分析:

这道题等价于:15次抽签,每次抽到蓝色的概率分别为1/2~1/16,问最终抽到蓝色比红色多的概率是多少,为多次独立事件求复合的概率问题。

而这又等价于求多项式(1/2*x+1/2)(2/3*x+1/3)(3/4*x+1/4)(4/5*x+1/5)(5/6*x+1/6)(6/7*x+1/7)(7/8*x+1/8)(8/9*x+1/9)(9/10*x+1/10)(10/11*x+1/11)(11/12*x+1/12)(12/13*x+1/13)(13/14*x+1/14)(14/15*x+1/15)(15/16*x+1/16)的x^7,x^6....x^0的系数和。相对于使用c++、java等工具,这道题更适合使用matlab计算。


解题过程:

一、多项式展开:

expand((1/2*x+1/2)*(2/3*x+1/3)*(3/4*x+1/4)*(4/5*x+1/5)*(5/6*x+1/6)*(6/7*x+1/7)*(7/8*x+1/8)*(8/9*x+1/9)*(9/10*x+1/10)*(10/11*x+1/11)*(11/12*x+1/12)*(12/13*x+1/13)*(13/14*x+1/14)*(14/15*x+1/15)*(15/16*x+1/16))
结果为:

x^15/16 + (1195757*x^14)/5765760 + (13215487*x^13)/44844800 + (35118025721*x^12)/145297152000 + (2065639*x^11)/15966720 + (277382447*x^10)/5748019200 + (2271089*x^9)/174182400 + (54576553*x^8)/20901888000 + (4783*x^7)/12192768 + (324509*x^6)/7315660800 + (109*x^5)/29030400 + (26921*x^4)/114960384000 + x^3/95800320 + (47*x^2)/149448499200 + x/174356582400 + 1/20922789888000

二、求和:

4783/12192768 + 324509/7315660800 + 109/29030400 + 26921/114960384000 + 1/95800320 + 47/149448499200 + 1/174356582400 + 7573384136472607/158456325028528675187087900672
结果为:

 4.4064e-04 即为最终挑战获胜的概率。


求倒数之后易知最高奖金应为2269




以上只是我做题时的解法。

如果有更好的解法、更好的思路,欢迎评论讨论~O(∩_∩)O~


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一份可能的 C++ 程序,它实现了你的要求: ```cpp #include <iostream> #include <fstream> #include <string> #include <vector> #include <algorithm> using namespace std; struct Employee { string name; int prize; }; bool comparePrize(const Employee& a, const Employee& b) { return a.prize > b.prize; } int main() { const string inputFile = "bg.in"; const string outputFile = "C:\\my\\bg.out"; vector<Employee> employees; int totalPrize = 0; ifstream fin(inputFile); if (!fin) { cerr << "Error: Failed to open input file." << endl; return 1; } string header; getline(fin, header); while (!fin.eof()) { string name; int prize; fin >> name >> prize; if (fin.fail()) { break; } employees.push_back({ name, prize }); totalPrize += prize; } fin.close(); sort(employees.begin(), employees.end() - 1, comparePrize); ofstream fout(outputFile); if (!fout) { cerr << "Error: Failed to open output file." << endl; return 1; } fout << header << endl; for (const auto& employee : employees) { fout << employee.name << '\t' << employee.prize << endl; } fout << "TOTAL\t" << totalPrize << endl; fout.close(); cout << "Done." << endl; return 0; } ``` 这份程序做了以下几件事情: 1. 定义了一个 `Employee` 结构体,表示一名员工的姓名和奖金额; 2. 定义了一个 `comparePrize` 函数,用来按奖金额从高到低排序; 3. 读取输入文件 `bg.in`,计算年终奖总额并存储每个员工的信息; 4. 对除最后一行以外的员工按奖金额从高到低排序; 5. 输出处理后的表格到输出文件 `C:\my\bg.out`。 你可以将输入文件 `bg.in` 放在程序可执行文件所在的目录下,并运行程序,它会自动将处理后的表格输出到指定的输出文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值