Java: Towers of Hanoi Variant
Consider the following variant of the towers of Hanoi prob...
Computers and Technology, 19.02.2020 23:04 cvalentine3199
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);
}
}
Answers: 2
Computers and Technology, 23.06.2019 02:30
Which component acts as a platform on which application software runs
Answers: 2
Computers and Technology, 23.06.2019 22:30
How many points do i need before i can send a chat
Answers: 1
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
History, 14.04.2021 18:30
History, 14.04.2021 18:30
Biology, 14.04.2021 18:30
Mathematics, 14.04.2021 18:30
English, 14.04.2021 18:30
Mathematics, 14.04.2021 18:30
Mathematics, 14.04.2021 18:30
Mathematics, 14.04.2021 18:30
History, 14.04.2021 18:30
Social Studies, 14.04.2021 18:30
History, 14.04.2021 18:30
English, 14.04.2021 18:30
Biology, 14.04.2021 18:30