Talking about sharing apples with children ...

Buy 100 apples, assuming there are 10 brothers and sisters in total, how to distribute apples to the brothers and sisters reasonably and fairly so that they will not fight or fight.

Claim:

  • Everyone must have at least one apple, otherwise children who do n’t have an apple may be unhappy
  • In order to ensure fairness, everyone has the same probability of getting an apple, and ca n’t favor any party
  • The apple must be scored and cannot be kept

Use 0 ~ 9 to represent 10 younger brothers and sisters, then in order to ensure that each person has at least one apple, first give them an apple to "guarante the bottom", and then start to randomly assign them

You one ... me one ...

#include <iostream>
#include <time.h>

using namespace std;

int main() {
    int a[10]= {1,1,1,1,1,1,1,1,1,1,};
    int rest = 100 - 10;
    srand((unsigned int) (time(NULL)));
    while (rest != 0) {
        int i = rand() % 10;
        a[i]++;
        rest--;
    }
    for (int j = 0; j < 10; ++j) {
        cout << a[j] << " ";
    }
    return 0;
}

Well ... just and reasonable.

Published 34 original articles · Like 10 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/weixin_41111088/article/details/105097123