subject

Java: Towers of Hanoi Variant

Consider the following variant of the towers of Hanoi problem, There are 2n discs of increasing size stored on the three poles. Initially all of the discs with odd size (1, 3,..., 2n-1) are piled on the left pole from top to bottom in increasing order of size; all of the discs with even size (2, 4, ..., 2n) are piled on the right pole. Write a program to provide instructions for moving the odd discs to the right pole and the even discs to the left pole, obeying the same rules as for towers of Hanoi (The towers of Hanoi provided below)

public class TowersOfHanoi {

// print out instructions for moving n discs to
// the left (if left is true) or right (if left is false)
public static void moves(int n, boolean left) {
if (n == 0) return;
moves(n-1, !left);
if (left) StdOut. println(n + " left");
else StdOut. println(n + " right");
moves(n-1, !left);
}

public static void main(String[] args) {
int n = Integer. parseInt(args[0]);
moves(n, true);
}

}

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 21.06.2019 20:50
What are the advantages of google cloud ?
Answers: 2
question
Computers and Technology, 23.06.2019 02:30
Which component acts as a platform on which application software runs
Answers: 2
question
Computers and Technology, 23.06.2019 22:30
How many points do i need before i can send a chat
Answers: 1
question
Computers and Technology, 24.06.2019 07:30
John recently worked on a project about various programming languages. he learned that though procedural language programs are useful, they have disadvantages too. what is a disadvantage of programs written in procedural languages? a. programs do not represent data complexity. b. programs take more time to execute. c. programs are prone to security threats. d. programs do not interface with multiple platforms.
Answers: 3
You know the right answer?
Java: Towers of Hanoi Variant

Consider the following variant of the towers of Hanoi prob...
Questions
question
History, 14.04.2021 18:30
question
Biology, 14.04.2021 18:30
question
Mathematics, 14.04.2021 18:30
question
Mathematics, 14.04.2021 18:30
question
History, 14.04.2021 18:30
question
Social Studies, 14.04.2021 18:30
question
History, 14.04.2021 18:30
question
English, 14.04.2021 18:30
Questions on the website: 13722360