Chapter 6 of Robert lafore, Java data structures and algorithms
/* 6.1 assume that you have bought a cheap handheld computer, but find that its built-in chip cannot be used for multiplication and can only be used for addition. To get rid of this dilemma, you need to write your own program, write a recursive method mult (), and its parameter number is X and the return value of Y is X multiplied by it. does addition work when this method calls itself or when it returns? 6.2 In Chapter 2 "binary tree", we will see that there are indeed two Subtrees in each of its nodes. If you use characters to draw a binary tree on the screen, you can have a node on the top layer, two nodes on the next layer, and four, eight, and 16 nodes. Below is a tree with 16 characters at the bottom: -------- X-----------X-------X-----X---X---X---X--X-X-X-X-X-X-X-XXXXXXXXXXXXXXXXX (note that the bottom line should move half the width to the right, but not in character mode .) You can use the recursive makebranches () method to draw this tree. The parameters of this method are left and right, which are the endpoints of the horizontal range. When you enter this program for the first time, left is equal to 0, and right is the number of characters in all rows (including short lines) minus one. Draw an X in the middle of the range of this line. Then this method is called twice: the range of the left half and the range of the right half at a time. Returns if the range is too small. You may want to put all the short-term and X in an array and display the array at one time. You may use the display () method. Compile main () and call makebranches () and display () to draw this tree. Allow main () to determine the width of each row (32, 64 or any other value ). Make sure that the array storing the displayed characters is not larger than the required space. What is the relationship between the number of rows (five in the figure) and the width of each row? 6.3 apply recursive algorithms to obtain the multiplication of a number, as described in the "Multiplication of a number" section near the end of this chapter. Write the recursive power () method and a main () method to test it. 6.4 write a program that can solve the problem of a backpack. You can specify the capacity of the backpack and the weight of a series of items to store these weight values in an array. Tip: the parameter of the recursive method knapsack () is the array subscript of the target weight and the starting position of the remaining item. 6.5 apply a recursive method to show all possible solutions for a group of people (K are selected by N people each time ). Compile the recursive showteams () method and a main () method to prompt the user to enter the number of people in the group and the number of people in the group. This serves as the showteams () parameter and then display all the combinations. */Package chap06; // ================================================ ======================================================/// programming job 6.1 public class multiplication {// multiplicand multiplier // multiply by public int multiply (INT multiplicand, int multiplier) {If (multiplier = 1) {return multiplicand;} else {return multiplicand + multiply (multiplicand, multiplier-1 );}} public static void main (string [] ARGs) {multiplication mutiplication = new multiplication (); int result = mutiplication. multiply (6, 8); system. out. println (result );}} // ================================================ ======================================
Package chap06; // ================================================ ===================================================/ /program job 6.2 Public class binarytreeshow {private char [] [] lines; // number the maximum number of characters displayed in a row. Public binarytreeshow (INT number) {int rows = 1; int numberdivide = number; // do not remove the number directly, numberwhile (numberdivide = numberdivide/2) is also used later )! = 0) {// relationship between the number of rows and the row rows ++; // 2 ^ (Row-1) = Number 2 ^ (5-1) = 16} // when number = 16, row = 5 lines = new char [rows] [number]; for (INT I = 0; I <rows; I ++) {// initialize the array for (Int J = 0; j <number; j ++) {lines [I] [J] = '-';}}} public void display () {for (INT I = 0; I <lines. length; I ++) {// initialize the array for (Int J = 0; j <lines [I]. length; j ++) {system. out. print (lines [I] [J]);} system. out. println () ;}} public void makebranches (INT left, int right) {int number = right-left + 1; int ROW = 0; int muberdivide = number; // do not remove the number directly. numberwhile (muberdivide = muberdivide * 2) <= lines [0] is also used later. length) {row ++; // calculate the current row number} If (number = 1) {// Base Value Condition lines [row] [left] = 'X'; return ;} else {int mid = (left + right)/2 + 1; lines [row] [Mid] = 'X'; makebranches (left, mid-1); makebranches (MID, right) ;}} public void maketree () {makebranches (0, lines [0]. length-1);} public static void main (string [] ARGs) {binarytreeshow = new binarytreeshow (16); binarytreeshow. maketree (); binarytreeshow. display ();}} // ================================================ ========================================================
Package chap06; // programming job 6.3 // mathematics, mathematical operation // int range [-2 ^ 32 ~ 2 ^ 31-1]-2147483648 to 2147483647 public class mathematics {// evaluate the power of Y x ^ y // if y is always an even number, // 2 ^ 8 = (2 * 2) ^ (8/2) = 4 ^ 4 // 4 ^ 4 = (4*4) ^ (4/2) = 16 ^ 2/16 ^ 2 = (16*16) ^ (2/2) = 256 ^ 1 // 256 ^ 1 = 256 // if y is an odd number, // 3 ^ 18 = (3*3) ^ (18/2) = 9 ^ 9 // 9 ^ 9 = 9*(9 ^ 8) = 9 * (9*9) ^ (8/2) = 9*(81 ^ 4) // 9*(81 ^ 4) = 9 * (81*81) ^ (4/2) = 9*(6561 ^ 2) // 9*(6561 ^ 2) = 9 * (6561*6561) ^ (2/2) = 9*43046721 ^ 1 // 9*43046721 ^ 1 = 9*43046721 public int power (int x, int y) {If (y = 1) {return X;} else {If (Y % 2 = 0) {return power (x * X, y/2);} else {return x * power (x * X, Y/2);} public static void main (string [] ARGs) {mathematics = new mathematics (); int result = mathematics. power (3, 18); system. out. println (result );}}
Package chap06; // ================================================ ===================================================/ /programming job 6.4 public class knapsack {private int [] weights; // Optional Weight private Boolean [] selects; // record whether public knapsack (INT [] weights) {This. weights = weights; selects = new Boolean [weights. length];} // find all possible solutions // total weight // public void knapsack (INT total, int index) {If (total <0 | Total> 0 & index> = weights. length) {// The base value. The return statement is not found.} If (Total = 0) {// The Base Value, locate the for (INT I = 0; I <index; I ++) {If (selects [I] = true) {system. out. print (weights [I] + "") ;}} system. out. println (); return;} selects [Index] = true; knapsack (total-weights [Index], index + 1); selects [Index] = false; knapsack (total, index + 1);} public static void main (string [] ARGs) {knapsack = new knapsack (New int [] {11, 8, 7, 6, 5, 4, 3}); knapsack. knapsack (20, 0 );}} // ================================================ ========================================================
Package chap06; // programming job 6.5 // method 1 public class group {private char [] persons; // all available members in the group private Boolean [] selects; // mark whether the selected member is public group (char [] persons) {This. persons = Persons; selects = new Boolean [persons. length];} public void showteams (INT teamnumber) {showteams (persons. length, teamnumber );} // ================================================ ==========/// find all possible solutions // total number of totalnuber users to choose from /// how many people need to be selected for teamnuber ======== ======================================================================/ out K of N people, represent all the solutions of (n, k) method parameters // evaluate (n, k) // obtain a solution when K is 0, n <K without solution/otherwise (n, k) --> (n-1, k-1) + (n-1, K) // ================================================ ========/// the column is as follows: select two from three people. The parameter is () // () --> () +) + () // () to obtain a solution. () No solution // () + () -->) + () // () + () --> () + () // () Get a solution, () No solution // so (3, 2) a total of 3 solutions // column for example: Select 3 from 3 people, the parameter is (3, 3) // (3, 3) --> () +) --> (0, 0) // (3, 3) there is a solution. =========== private void showteams (INT totalnumber, int teamnumber) {If (teamnumber = 0) {// teamnumber = 0, find a solution for (INT I = 0; I <selects. length; I ++) {If (selects [I] = true) {system. out. print (persons [I] + "") ;}} system. out. println (); return;} If (totalnumber <teamnumber) {// totalnuber <teamnumber, no return;} selects [persons. length-totalnumber] = true; showteams (totalnumber-1, teamnumber-1); selects [persons. length-totalnumber] = false; showteams (totalnumber-1, teamnumber);} public static void main (string [] ARGs) {group Group = new group (New char [] {'A', 'B', 'C', 'D', 'E'}); group. showteams (3 );}}
Package chap06; // program job 6.5 // method 2 public class group1 {private char [] persons; // all available members in the group private Boolean [] selects; // mark whether the selected member is public group1 (char [] persons) {This. persons = Persons; selects = new Boolean [persons. length];} public void showteams (INT teamnumber) {showteams (teamnumber, 0 );} // ================================================ ==========/// find all possible solutions // number of players to be selected for teamnumber // index start from the number of players ==================================================================== // retrieve the possible scheme of K persons from n persons // The method parameters here have some changes (n, k) should be written as (K, I) (I = 0, 1, 2 ,...) // get a solution when k = 0, I> = n no solution // otherwise (K, I) --> (K-1, I + 1) + (K, I + 1) // ================================================ ========/// the column is as follows: select two from three, And the // parameter should be written as () // () --> () +) + () // () obtain a solution // () + () --> () +) + () // () one solution, () No solution, () No solution, two) // so (2, 0) three solutions are available. ============= private void showteams (INT teamnumber, int index) {If (teamnumber = 0) {// when teamnuber = 0, find for (INT I = 0; I <selects. length; I ++) {If (selects [I] = true) {system. out. print (persons [I] + "") ;}} system. out. println (); return;} If (index> = persons. length) {// index exceeds the number of persons to be selected, and return is not found;} selects [Index] = true; showteams (teamnumber-1, index + 1 ); selects [Index] = false; showteams (teamnumber, index + 1);} public static void main (string [] ARGs) {group1 group = new group1 (New char [] {'A', 'B', 'C', 'D', 'E'}); group. showteams (3 );}}