通过减少数量将x分成y个部分 [英] Divide x into y parts by decreasing amount

查看:102
本文介绍了通过减少数量将x分成y个部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有$ 1000(可变),并且我想将这笔钱分配给20个(可变)人,但不是平均分配给每个人,我想给第一个人更多,第二人称,等等.

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 person, and the 2nd person, etc.

因此,第20个人的人数最少,而第5个人的人数最多.

So the 20th person gets the least, and the 5th person gets the 5th most.

我将如何实现?

谢谢

公式:

int people = 20;
float prize = 1000;

float k = (2 * prize) / ((people) * (people - 1));
float sum = 0;

for (int i = 1; i < people; ++i)
{
    var personsPrize = i * k;
    sum += personsPrize;
    Console.WriteLine(personsPrize);
}
Console.WriteLine("sum = " + sum);

推荐答案

公式是正确的,需要稍加触摸.

Formula is correct, needed a little touch.

  1. 不要将float转换为int,数据丢失!
  2. 在从第一人称进入n-1的for内进入

  1. Don't cast float into int, data loss!
  2. When going within the for go from the first person to the n-1

int people = 20;
float prize = 1000;

float k = (2 * prize) / ((people) * (people - 1));
float sum = 0;

for (int i = 1; i < people; ++i)
{
    var personsPrize = i * k;
    sum += personsPrize;
    Console.WriteLine(personsPrize);
}
Console.WriteLine("sum = " + sum);

这篇关于通过减少数量将x分成y个部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆