Divide x into y parts by decreasing amount

后端 未结 3 2086

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:23

    Formula is correct, needed a little touch.

    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);
      

提交回复
热议问题