Divide x into y parts by decreasing amount

后端 未结 3 2091

If I had $1000(variable) and I want to split that amount up and give it to 20(variable) people, but rather than give it evenly to each person, I want to give more to the 1st per

3条回答
  •  忘了有多久
    2021-01-25 22:28

    Adding answer to Ensure prize pool doesn't award tied participants less than participants who scored worse

    Two things

    1. Incorrect, when assigning prices start from first person to the n-1 one person does't get prize, correct is start from first person to the n

    2. Valid basePrize noted with K is

      k = (2 * prize) / ((people) * (people + 1))

    Now the answer the other question:

    int totalPeople = 20;
    float prize = 5000;
    
    List peopleScores = new List();
    Random r = new Random();
    for (int i = 0; i < totalPeople; ++i)
    {
        peopleScores.Add(r.Next(0, 100));
    }
    
    var totalPeopleWithScore = peopleScores.Where(x => x > 0).Count();
    
    var groupedScores = peopleScores
        .Where(x => x > 0)
        .ToList()
        .GroupBy(x => x)
        .Select(grp => new
        {
            score = grp.Key,
            peopleScores = grp.ToList()
        })
        .OrderBy(g => g.score)
        .ToList();
    
    var groupCount = groupedScores.Select(x => new { count = x.peopleScores.Count() }).Select(z => z.count).Count();
    
    float basePrizeRate = 2 * prize / totalPeopleWithScore / (totalPeopleWithScore + 1);
    Console.WriteLine("Base Price rate: " + basePrizeRate);
    float sum = 0;
    var leaderboardPosition = 0;
    var totalWinners = 0;
    foreach (var positionScore in groupedScores)
    {
        var countWinner = positionScore.peopleScores.Count();
        Console.WriteLine();
        Console.WriteLine($"On leaderboard position : {groupedScores.Count() - leaderboardPosition} are {countWinner} winners with score: {positionScore.score}");
    
        float positionPrizePool = 0;
        for (int i = 1; i <= positionScore.peopleScores.Count(); ++i)
        {
            totalWinners++;
            positionPrizePool += totalWinners * basePrizeRate;
        }
        Console.WriteLine("Prize Pool " + positionPrizePool);
        var personPoolPrize = positionPrizePool / positionScore.peopleScores.Count();
        foreach (var x in positionScore.peopleScores)
        {
            Console.WriteLine($"Winner {totalWinners} won: {personPoolPrize}");
            sum += personPoolPrize;
        }
        leaderboardPosition++;
    }
    Console.WriteLine();
    
    Console.WriteLine("Total Prize: " + sum);
    Console.WriteLine("Total Winners: " + totalPeopleWithScore);
    

提交回复
热议问题