The sum of n numbers is 32898.

Source: Internet
Author: User
The sum of the known n numbers is 32898, and the sum of the known n numbers of each addition is 32898 or another integer,
Calculate the number of each addition. random results are required.
An integer between 1 (inclusive) and 999 (inclusive)
That is, $ arr [1] + $ arr [2] +... + $ arr [n] = 32898

The following 33 adders must add up to 32898 and calculate the 33 adders. an array of the 33 adders is required to be returned. Random results are required.

In my actual application, the number of each addition must be between 1 and 999. please be sure to test according to this interval.

function xxx(32898,33,1,999){    $arr = array();     return $arr;}


Reply to discussion (solution)

Can the addons be repeated?

33 1 ~ 32898 in number of 99
The average is 997.
So the value range is 995 ~ 999
It is impossible to get 1.

$ R = foo (32898, 33); echo array_sum ($ r), PHP_EOL; // verify the sum of print_r (array_count_values ($ r )); // view the distribution function foo ($ num, $ k, $ min = 1, $ max = 999) {$ res = array_fill (0, $ k, 1 ); do {for ($ I = 0; $ I <$ k; $ I ++) {$ sum = array_sum ($ res); $ t = rand (0, $ max-$ min); if ($ res [$ I] + $ t> $ max) $ t = $ max-$ res [$ I]; if ($ sum + $ t> $ num) $ t = $ num-$ sum; $ res [$ I] + = $ t ;}} while ($ num> $ sum); return $ res ;}
A possible result
32898Array(    [999] => 31    [971] => 1    [958] => 1)

Can the addons be repeated?



The number of addons can be repeated. But try not to repeat it a little better.

$ R = foo (32898, 33); echo array_sum ($ r), PHP_EOL; // verify the sum of print_r (array_count_values ($ r )); // view the distribution function foo ($ num, $ k, $ min = 1, $ max = 999) {$ res = array_fill (0, $ k, 1 ); do {for ($ I = 0; $ I <$ k; $ I ++) {$ sum = array_sum ($ res); $ t = rand (0, $ max-$ min); if ($ res [$ I] + $ t> $ max) $ t = $ max-$ res [$ I]; if ($ sum + $ t> $ num) $ t = $ num-$ sum; $ res [$ I] + = $ t ;}} while ($ num> $ sum); return $ res ;}
A possible result
32898Array(    [999] => 31    [971] => 1    [958] => 1)



$ R = foo (32898, 33); echo array_sum ($ r), PHP_EOL; // verify the sum of print_r (array_count_values ($ r )); // view the distribution function foo ($ num, $ k, $ min = 1, $ max = 999) {$ res = array_fill (0, $ k, 1 ); do {for ($ I = 0; $ I <$ k; $ I ++) {$ sum = array_sum ($ res); $ t = rand (0, $ max-$ min); if ($ res [$ I] + $ t> $ max) $ t = $ max-$ res [$ I]; if ($ sum + $ t> $ num) $ t = $ num-$ sum; $ res [$ I] + = $ t ;}} while ($ num> $ sum); return $ res ;}
A possible result
32898Array(    [999] => 31    [971] => 1    [958] => 1)



After testing the number of 53, the answer is completely correct. if the number is 33, can the number of 33 be output? if the number is 53, the number of 53 is output, instead of the current one. Thank you.

20 Points Added. thank you.

$ R = fool (32898, 33 );
$ R is the result you want. I just did the verification. What are you going to do with him? it's your own business.
$ R is an array, and print_r ($ r) will see the content.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.