gpt4 book ai didi

c++ - 为锦标赛系统分配奖品

转载 作者:太空狗 更新时间:2023-10-29 20:31:50 26 4
gpt4 key购买 nike

我正在寻找一种将数字分配给 x 个单位的方法。我什至不知道怎么写这个词所以我举个例子:

有一个总奖金为 1000 美元的锦标赛。我希望前 20 名获胜者/参赛者能从中赢得一些东西。
我需要一个数学算法/公式来将它分配给这些玩家,并让我能够控制分配的某些其他因素。

例如,我希望排名第一的获胜者将获得 300 美元。排名第二的获胜者将获得较小的比例。总分配必须给每个人一些东西,直到前 20 名获胜者(最后一个)将至少获得 X$。
X$ 是我要控制的另一个因素。

有什么想法吗?这个问题有名字吗(那个名字是什么)?有代码示例吗?

编辑 #1 - 我的第一个提案:

#include <conio.h>
#include <vector>

#define TOTAL 100
#define WINNERS 15
#define FIRST_WINNER_PERCENTAGE 0.30

void distribute_1(::std::vector<double> * const prizes)
{
prizes->clear();

double total = TOTAL;
double winning_percentage = FIRST_WINNER_PERCENTAGE;
double slope = 0.5;
int winners = WINNERS;

double winning = 0;
for(int i = 0; i < winners; i++, total -= winning, winning_percentage /= 2)
{
winning = total * winning_percentage;
prizes->push_back(winning);
}
}
void distribute_2(::std::vector<double> * const prizes)
{
prizes->clear();

double total = TOTAL;
double winning_percentage = FIRST_WINNER_PERCENTAGE;
double slope = 0.5;
int winners = WINNERS;

double winning = 0;
for(int i = 0; i < winners; i++, total -= winning/*, winning_percentage /= 2*/)
{
winning = total * winning_percentage;
prizes->push_back(winning);
}
}
void distribute_3(::std::vector<double> * const prizes)
{
prizes->clear();

double total = TOTAL;
double winning_percentage = FIRST_WINNER_PERCENTAGE;
double slope = 0.0005;
int winners = WINNERS;

double winning = 0;
for(int i = 0; i < winners; i++, total -= winning, winning_percentage -= slope)
{
winning = total * winning_percentage;
prizes->push_back(winning);
}
}
void distribute_4(::std::vector<double> * const prizes)
{
prizes->clear();

double total = TOTAL;
double winning_percentage = FIRST_WINNER_PERCENTAGE;
double slope = 1 / WINNERS;
int winners = WINNERS;

double winning = 0;
for(int i = 0; i < winners; i++, total -= winning, winning_percentage -= slope)
{
winning = total * winning_percentage;
prizes->push_back(winning);
}
}

void main()
{
::std::vector<double> prizes;

distribute_1(&prizes);
distribute_2(&prizes);
distribute_3(&prizes);
distribute_4(&prizes);

double total_granted = 0;
for(int i = 0; i < WINNERS; i++)
{
total_granted += prizes[i];
printf("%lf\n", prizes[i]);
}
printf("-\n%lf\n", total_granted);

_getch();
}

这是我能达到的极限。例如,这个问题是,如果您将“WINNERS”设置为 5,则算法没有达到“TOTAL”数量(本例中为 100)或更接近(我总共得到 83) .

Cristy 的解决方案:

#include <conio.h>
#include<iostream>
//using arithmetic progression
using namespace std;
int i;
float ratio;
float first_prize;
float s;
int main()
{
float money=1000;
const int total_prizes = 10;
float last_prize = 99;
float prizes[total_prizes+1];

/**/first_prize=2*money/total_prizes-last_prize; //last member of the progresion
ratio=(first_prize-last_prize)/(total_prizes-1);
prizes[total_prizes]=last_prize;
for(i=total_prizes-1;i>=1;i--){
prizes[i]=prizes[i+1]+ratio;
money-=prizes[i];
}
for(i=1;i<=total_prizes;i++){
printf("%d) %.2f\n",i,prizes[i]);
s+=prizes[i];
}
printf("TOTAL SUM:%.2f\n",s);
printf("Ratio: %.2f", ratio);
_getch();
}

最佳答案

现在是凌晨 1:15,我正在解决数学问题 :))。
使用算术级数。
我使用定义制作了所有内容,因此您可以轻松更改它们。

#include<iostream>
//using arithmetic progression
using namespace std;
FILE *g=fopen("output.out","w");
#define last_prize 10
#define total_prizes 20
int i;
float prizes[total_prizes+1];
float money=1000;
float ratio;
float first_prize;
float s;
//a1=last_prize
//an=first_prize
int main(){
first_prize=2*money/total_prizes+last_prize; //last member of the progresion
ratio=(first_prize-last_prize)/(total_prizes-1);
prizes[total_prizes]=last_prize;
for(i=total_prizes-1;i>=1;i--)
prizes[i]=prizes[i+1]+ratio;
for(i=1;i<=total_prizes;i++){
fprintf(g,"%d) %.2f\n",i,prizes[i]);
s+=prizes[i];
}
fprintf(g,"TOTAL SUM:%.2f",s);
return 0;
}

输出:

1) 90.00
2) 85.79
3) 81.58
4) 77.37
5) 73.16
6) 68.95
7) 64.74
8) 60.53
9) 56.32
10) 52.11
11) 47.89
12) 43.68
13) 39.47
14) 35.26
15) 31.05
16) 26.84
17) 22.63
18) 18.42
19) 14.21
20) 10.00
TOTAL SUM:1000.00

如您所见,它们加起来正好是 1000.00$ :D

其他结果:
输入:

#define last_prize 30
#define total_prizes 5

输出:

1) 370.00
2) 285.00
3) 200.00
4) 115.00
5) 30.00
TOTAL SUM:1000.00

关于c++ - 为锦标赛系统分配奖品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3409116/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com