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:
- public Interface Monkeysmovebananas {
- /**
- *
- * Total number of @param num
- * @param maxnum The maximum number of moves such as 50 each
- * @param distance distance
- * @param eatnum per metre of food, such as 1 .
- *
- * @return The remaining quantity in the home, or the quantity remaining when the banana cannot be moved
- *
- */
- int Move(int num, int maxnum, int distance, int eatnum);
- }
Implementation class:
- /**
- * @category monkeys to move bananas.
- * @author song
- */
- public class Monkeysmovebananasimpl implements Monkeysmovebananas {
- /**
- *
- * Total number of @param num
- * @param maxnum The maximum number of moves such as 50 each
- * @param distance distance
- * @param eatnum per metre of food, such as 1 .
- *
- * @return The remaining quantity in the home, or the quantity remaining when the banana cannot be moved
- *
- */
- @Override
- Public int Move(int num, int maxnum, int distance, int eatnum) {
- / * has moved to the location, that is, moved a few meters * *
- int location = 0;
- / * The number of points left at the start of each handling * *
- int currentnumbers = num;
- / * Quantity left at each end of handling * /
- int currentnumbere = 0;
- / * Outer loop once, i.e. move all remaining bananas one position * /
- While (location < distance && Currentnumbers > Eatnum) {
- System.out.println ("Current position" + location + "with" + currentnumbers + "banana");
- / * Inner Loop once, that is, to move the remaining banana one position, is to move a position of the specific details * /
- While (true) {
- if (Currentnumbers > Maxnum) {
- Moveoneposition (location, location+1, Maxnum, eatnum);
- Currentnumbers-= Maxnum;
- Currentnumbere + = Maxnum-eatnum;
- } Else {
- Moveoneposition (location, location+1, currentnumbers, eatnum);
- Currentnumbere + = Currentnumbers-eatnum;
- Currentnumbers = 0;
- }
- / * See if you need to move from the beginning to the left, i.e. need not return * /
- if (currentnumbers >= eatnum * 2) {
- Moveoneposition (location+1, location,-1, eatnum);
- Currentnumbere-= Eatnum;
- } Else {
- Break ;
- }
- }
- Currentnumbers = Currentnumbere;
- Currentnumbere = 0;
- Location + +;
- }
- System.out.println ("remainder" + currentnumbers + "one");
- return currentnumbers;
- }
- /**
- *
- * @param from start position
- * @param to target location
- * number of @param numbers moved
- * @param eatnum to move one metre to eat the quantity
- */
- private void moveoneposition(int from, int to, int number, int eatnum) {
- if (number! =-1) {
- System.out.println ("+from +" position "+ number + " to "+ to + " position, consumption "+ Eatnum + ");
- } Else {
- System.out.println ("+ from +" position back "+ to + " position, consumption "+ Eatnum + ");
- }
- }
- }
Test class:
- public class monkeysmovebananastest {
- private static Monkeysmovebananas Monkeysmovebananas = null;
- @BeforeClass
- Public static void setupbeforeclass() throws Exception {
- Monkeysmovebananas = new Monkeysmovebananasimpl ();
- }
- @Test
- Public void movetest() {
- int k = monkeysmovebananas.move ( 1);
- Assertequals (k, + );
- K = Monkeysmovebananas.move (+, 2);
- Assertequals (k );
- }
- }
Algorithm-Monkey Move banana