Enter two integers, n and m, from the numbers 1, 2, 3 ,...... Random Number in n to make it equal

Source: Internet
Author: User
Question 21: programming, input two integers n and m, from Series 1, 2, 3 ,...... N is random, so that the sum is equal to m. All possible combinations are required. It is actually a backpack problem. Solution: 1. First, determine that if nm is used, the number of n greater than m cannot be involved in the combination, and then set it to nm; 2. Add the maximum number n to the nm,

Question 21: programming, input two integers n and m, from Series 1, 2, 3 ,...... N is random, so that the sum is equal to m. All possible combinations are required. It is actually a backpack problem. Solution: 1. first, it is judged that, if the nm is used, the number of n greater than m cannot be involved in the combination, and n = m; 2. add the maximum number n and n = m,

Question 21: programming, input two integers n and m, from Series 1, 2, 3 ,...... N is random, so that the sum is equal to m. All possible combinations are required. It is actually a backpack problem.

Solution:

1. First, it is judged that if n> m, the number of n greater than m cannot be involved in the combination, and n = m is set at this time;

2. If the maximum number n is added and n = m, the condition is met and the result is output;

3. solve n in two cases: (1) n is not added, n = n-1; m = m; recursion; (2) n is added, take n = n-1l, m = m-n, and Recursion

[Java]View plaincopyprint?

  1. Public class s21 {
  2. Private static upload list List = new external list ();
  3. /**
  4. * Solution:
  5. * 1. First, it is judged that if n> m, the number of n greater than m cannot be involved in the combination, and n = m is set at this time;
  6. * 2. If the maximum number n is added and n = m, the condition is met and the output is successful;
  7. * 3. Solve n in two cases: n is not added, n = n-1, m = m, and recursion;
  8. * 4. Add n, n = n-1, m = m-n, and recursion.
  9. * 5. End.
  10. * @ Param sum
  11. * @ Param n
  12. */
  13. Public static void findSum (int sum, int n)
  14. {
  15. If (n <1 | sum <1)
  16. Return;
  17. If (sum> n)
  18. {
  19. List. add (n );
  20. FindSum (sum-n, n-1); // Add n, n = n-1, m = m-n
  21. List. pop ();
  22. FindSum (sum, n-1); // n is not added, n = n-1, m = m
  23. }
  24. Else
  25. {
  26. System. out. print (sum); // sum <n, directly output n to meet
  27. For (int I = 0; I <list. size (); I ++)
  28. System. out. print ("" + list. get (I ));
  29. System. out. println ();
  30. }
  31. }
  32. /**
  33. * @ Param args
  34. */
  35. Public static void main (String [] args ){
  36. // TODO Auto-generated method stub
  37. Int sum = 10;
  38. Int n = 6;
  39. FindSum (sum, n );
  40. }
  41. }

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.