Algorithm-Monkey Move banana

Source: Internet
Author: User

Algorithm-Monkey Move bananaJanuary 28, 2018 19:59:15Number of readings:Problem Description:

There are 100 bananas on the edge of a small monkey, it will take 50 meters to get home, each time it moves up to 50 bananas. It will eat a root every 1 meters, how many bananas it can move to the home?

Tip: He can put the banana down for a round trip, but must make sure it has bananas to eat every metre of the walk. can also go to n meters, put down some bananas, take the N Banana walk back to move 50.

Program implementation:

Interface:
  1. public Interface Monkeysmovebananas {
  2. /**
  3. *
  4. * Total number of @param num
  5. * @param maxnum The maximum number of moves such as 50 each
  6. * @param distance distance
  7. * @param eatnum per metre of food, such as 1 .
  8. *
  9. * @return The remaining quantity in the home, or the quantity remaining when the banana cannot be moved
  10. *
  11. */
  12. int Move(int num, int maxnum, int distance, int eatnum);
  13. }


Implementation class:
  1. /**
  2. * @category monkeys to move bananas.
  3. * @author song
  4. */
  5. public class Monkeysmovebananasimpl implements Monkeysmovebananas {
  6. /**
  7. *
  8. * Total number of @param num
  9. * @param maxnum The maximum number of moves such as 50 each
  10. * @param distance distance
  11. * @param eatnum per metre of food, such as 1 .
  12. *
  13. * @return The remaining quantity in the home, or the quantity remaining when the banana cannot be moved
  14. *
  15. */
  16. @Override
  17. Public int Move(int num, int maxnum, int distance, int eatnum) {
  18. / * has moved to the location, that is, moved a few meters * *
  19. int location = 0;
  20. / * The number of points left at the start of each handling * *
  21. int currentnumbers = num;
  22. / * Quantity left at each end of handling * /
  23. int currentnumbere = 0;
  24. / * Outer loop once, i.e. move all remaining bananas one position * /
  25. While (location < distance && Currentnumbers > Eatnum) {
  26. System.out.println ("Current position" + location + "with" + currentnumbers + "banana");
  27. / * Inner Loop once, that is, to move the remaining banana one position, is to move a position of the specific details * /
  28. While (true) {
  29. if (Currentnumbers > Maxnum) {
  30. Moveoneposition (location, location+1, Maxnum, eatnum);
  31. Currentnumbers-= Maxnum;
  32. Currentnumbere + = Maxnum-eatnum;
  33. } Else {
  34. Moveoneposition (location, location+1, currentnumbers, eatnum);
  35. Currentnumbere + = Currentnumbers-eatnum;
  36. Currentnumbers = 0;
  37. }
  38. / * See if you need to move from the beginning to the left, i.e. need not return * /
  39. if (currentnumbers >= eatnum * 2) {
  40. Moveoneposition (location+1, location,-1, eatnum);
  41. Currentnumbere-= Eatnum;
  42. } Else {
  43. Break ;
  44. }
  45. }
  46. Currentnumbers = Currentnumbere;
  47. Currentnumbere = 0;
  48. Location + +;
  49. }
  50. System.out.println ("remainder" + currentnumbers + "one");
  51. return currentnumbers;
  52. }
  53. /**
  54. *
  55. * @param from start position
  56. * @param to target location
  57. * number of @param numbers moved
  58. * @param eatnum to move one metre to eat the quantity
  59. */
  60. private void moveoneposition(int from, int to, int number, int eatnum) {
  61. if (number! =-1) {
  62. System.out.println ("+from +" position "+ number + " to "+ to + " position, consumption "+ Eatnum + ");
  63. } Else {
  64. System.out.println ("+ from +" position back "+ to + " position, consumption "+ Eatnum + ");
  65. }
  66. }
  67. }


Test class:
  1. public class monkeysmovebananastest {
  2. private static Monkeysmovebananas Monkeysmovebananas = null;
  3. @BeforeClass
  4. Public static void setupbeforeclass() throws Exception {
  5. Monkeysmovebananas = new Monkeysmovebananasimpl ();
  6. }
  7. @Test
  8. Public void movetest() {
  9. int k = monkeysmovebananas.move ( 1);
  10. Assertequals (k, + );
  11. K = Monkeysmovebananas.move (+, 2);
  12. Assertequals (k );
  13. }
  14. }

Algorithm-Monkey Move banana

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.