DWQA QuestionsCategory: BlockchainWhat kind of algorithm can complete the payment within the set total amount and specified times

For example, if the total amount is 100 and 50 pieces are required to be exchanged, and the amount of each piece is between 1-2.5 (which can be equal to the threshold value), how can 100 yuan be paid out in 50 times?

1 first, the average allocation is 100/50 = 2, and stored in the array 
2 loop several times (more than 50 times), random array item
The amount that can be subtracted downward is add = X-1, and the amount that can be increased upward is sub = 2.5-x
Determine whether it is up or down randomly, take the random value of subtracting 0-sub or increase quota by 0-add, and record the adjustment value as delta
Take another item in the array at random and perform the reverse operation to increase or subtract Delta. If it is out of range, keep the difference and continue to adjust until delta = 0

At present, it is similar to this practice. First, n (50) – 1 numbers are randomly selected, and the sum of the last direct total amount minus (n-1) equals to the difference value, and then the cycle is + / -; until the difference value is 0

``````Const red packet allocation = (total amount, total amount, range) = >{
Const [min, Max] = range
Let allocation result = []
Let allocated amount = 0
Total number of lets allocated = 0
While (total allocated < total - 1){
Let allocation amount
do {
Distribution amount = minimum value+ Math.random () * (max min)
Const remaining average = (total - allocated amount - allocated amount) / (total - Total allocated - 1)
If (residual average > minimum * 1.01 & residual average < maximum *. 99){
break;
}
} while (true)

Distribution result.push (distribution amount)
Allocated amount + = allocated amount
Total allocated++
}
Distribution result.Push
Return allocation result
}

console.log (red packet allocation (100, 50, [1, 2.5]). Map ((n, I) = > (I & & I% 5 = = = 0? ', n': ') + n.tofixed (2)). Join (', '))`````` It seems that there is a problem. I do the test: the minimum value is 4, the maximum value is 38, the total amount is 84, and the number of items is 12. After several times of execution, the first one gets more than 30, which is not average enough

@boy_58060114a8412I just tried it a few times, and I didn’t find it

@ cloud fragranceData: 23.88, 18.77, 12.39, 36.80, 5.99, 24.18, 24.71, 11.25, 18.41, 4.48, 27.63, 31.85, 12.53, 4.46, 7.38, 6.50, 4.26, 4.01, 4.21, 4.05, 4.03, 4.02, 4.09, 4.01, 4.06, 4.07, 4.04

Yes, sometimes it’s a little over the boundary. In most cases, it’s more average

``````    // java
static double START = 1.5d;
static double END = 2.5d;

public static void main(String[] args) {~~~~
double amount = 100d;
int n = 50;
double start = START;
double end = END;
double remain = amount;
double total = 0;
List<Double> list = new ArrayList<>();

for (int i = 0; i < n - 1; i++) {
double d = RandomUtils.nextDouble(start, end);
remain -= d;
total += d;
System.out.println(d);
double ar = remain / (50d - i - 1);

start = Math.max(ar - 0.5d, START);
end = Math.min(ar + 0.5d, END);
}

System.out.println(remain);
total += remain;

System.out.println(list.size());
System.out.println(total);
}
`````` This is a middle school problem
Let me think about it first.

I forgot that. I feel a little familiar

@boy_58060114a8412I’m good at Java

@acd28705Java can