Computers and Technology

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);
}

}

answer
Answers: 2

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 20:40, broang23
Write a program that begins by reading in a series of positive integers on a single line of input and then computes and prints the product of those integers. integers are accepted and multiplied until the user enters an integer less than 1. this final number is not part of the product. then, the program prints the product. if the first entered number is negative or 0, the program must print β€œbad input.” and terminate immediately. next, the program determines and prints the prime factorization of the product, listing the factors in increasing order. if a prime number is not a factor of the product, then it
Answers: 2
image
Computers and Technology, 23.06.2019 09:00, mimithurmond03
Which is the highest level of the hierarchy of needs model? a. humanity b. intrapersonal c. team d. interpersonal
Answers: 1
image
Computers and Technology, 23.06.2019 09:00, Riddledjam44623
Before you record your own voice, you should a. record other people's voices b. warm up and practice difficult names c. listen to your favorite songs d. read a transcript of a good radio news segment
Answers: 1
image
Computers and Technology, 23.06.2019 09:00, paulusl19
The first screen you see when you open word2016 what is called?
Answers: 1
Do you know the correct answer?
Java: Towers of Hanoi Variant

Consider the following variant of the towers of Hanoi prob...

Questions in other subjects:

Konu
Biology, 11.02.2022 02:00
Konu
Computers and Technology, 11.02.2022 02:00
Konu
Mathematics, 11.02.2022 02:00