算法和数据结构 作业
1. 使用JAVA编写一个递归方法, 返回整数N的二进制表示中1的个数. (提示: 如果N是奇数, 那它等于N/2的二进制表示中1的个数加1.)
2. Write a method to recursively creates a String that is the binary representation of an int N.
public String getBinaryString (int n);
Here is an example. The binary equivalent of 13 may be found by repeatedly dividing 13 by 2. If there is a remainder a 1 gets concatenated to the resulting String otherwise a zero gets concatenated to the resulting String. (The tricky part is figuring out the right time to do the concatenation.)
13 / 2 = 6 r 1
6 / 2 = 3 r 0
3 / 2 = 1 r 1
1 / 2 = 0 r 113 in base 2 is 1101. The method would return "1101".3. Write the methods with the following declarations:
public void permute (String str);
public void permute (char[] str, int pos, int length);The first method is a driver that calls the second and prints all the permutations of the characters in String str. If str is "abc", then the strings that are output are abc, acb, bac, bca, cab, and cba. Use recursion for the second method.
You need to encapsulate these two methods into a class. Include your testing result too.4. Design a generic class, Collection, that stores a collection of Object (in an array), along with the current size of the collection. Provide public methods with declaration as follows: //check if the collection is empty.
public Boolean isEmpty( ); //make the collection empty
public void makeEmpty; //insert an object into the collection
// you may have to expand your array if needed
public void insert(Object str); // remove the obj at index i from the collection
//you need to shrink the array if the array is over half empty and the size is greater than 10.
public void remove(int i); // returns true if and only if an Object that is equals //with the obj is present in the collection.
public boolean isPresent(Object obj); 5. Write a Java program to solve the Hanoi tower problem with recursion. Your class should named HanoiTower.java. The class should contain a static method with signature as follows:
public void solveHanoiTower( int numOfDisk,
char fromPole, char withPole, char toPole); The method should print out messages like follows if there are 2 disks to move.)
Move disk 1 from pole A to pole B;
Move disk 2 from pole A to pole C;
Move disk 1 from pole B to pole C;
1. 使用JAVA编写一个递归方法, 返回整数N的二进制表示中1的个数. (提示: 如果N是奇数, 那它等于N/2的二进制表示中1的个数加1.)
2. Write a method to recursively creates a String that is the binary representation of an int N.
public String getBinaryString (int n);
Here is an example. The binary equivalent of 13 may be found by repeatedly dividing 13 by 2. If there is a remainder a 1 gets concatenated to the resulting String otherwise a zero gets concatenated to the resulting String. (The tricky part is figuring out the right time to do the concatenation.)
13 / 2 = 6 r 1
6 / 2 = 3 r 0
3 / 2 = 1 r 1
1 / 2 = 0 r 113 in base 2 is 1101. The method would return "1101".3. Write the methods with the following declarations:
public void permute (String str);
public void permute (char[] str, int pos, int length);The first method is a driver that calls the second and prints all the permutations of the characters in String str. If str is "abc", then the strings that are output are abc, acb, bac, bca, cab, and cba. Use recursion for the second method.
You need to encapsulate these two methods into a class. Include your testing result too.4. Design a generic class, Collection, that stores a collection of Object (in an array), along with the current size of the collection. Provide public methods with declaration as follows: //check if the collection is empty.
public Boolean isEmpty( ); //make the collection empty
public void makeEmpty; //insert an object into the collection
// you may have to expand your array if needed
public void insert(Object str); // remove the obj at index i from the collection
//you need to shrink the array if the array is over half empty and the size is greater than 10.
public void remove(int i); // returns true if and only if an Object that is equals //with the obj is present in the collection.
public boolean isPresent(Object obj); 5. Write a Java program to solve the Hanoi tower problem with recursion. Your class should named HanoiTower.java. The class should contain a static method with signature as follows:
public void solveHanoiTower( int numOfDisk,
char fromPole, char withPole, char toPole); The method should print out messages like follows if there are 2 disks to move.)
Move disk 1 from pole A to pole B;
Move disk 2 from pole A to pole C;
Move disk 1 from pole B to pole C;
http://lijie250.javaeye.com/blog/94380
让我好好想想