Binary number

Topic link

https://www.nowcoder.com/practice/103dd589fed14457a673c613d8de3841?tpId=67&tqId=29634&tPage=1&ru=%2Fkaoyan%2Fretest%2F1005&qru=%2Fta%2Fbupt -kaoyan%2Fquestion-ranking

Analysis of ideas

Use java directly Integer Built in toBinaryString Function can

AC code

 import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Integer string = scanner.nextInt(); System.out.println(Integer.toBinaryString(string)); } }

Huffman tree

Topic link

https://www.nowcoder.com/practice/162753046d5f47c7aac01a5b2fcda155?tpId=67&tqId=29635&tPage=1&ru=%2Fkaoyan%2Fretest%2F1005&qru=%2Fta%2Fbupt -kaoyan%2Fquestion-ranking

Analysis of ideas

Find that the weighted path length of the Huffman tree=the sum of the weighted path lengths of all leaf nodes.

The weighted path length of each node=the weight value of the node * the path length (the number of branches from the root to the node)

If we follow the above idea, we must build a complete Harman tree. It is troublesome to calculate the number of levels of each node and traverse it hierarchically.

The weighted path length of the tree=the sum of the weights of all non leaf nodes.

So the key problem is how to calculate the weight of all non leaf nodes.

To build a Harman tree, each time we combine two minimum weight nodes into a non leaf node (the weight of non leaf nodes is the sum of the weights of the previous two nodes). So you can use Java's PriorityQueue Priority queue. To join the priority queue, although the internal order of the queue is unclear, the elements of each queue (the head of the queue) are minimal.

AC code

 import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.PriorityQueue; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); PriorityQueue<Integer> list = new PriorityQueue<Integer>(); for (int i = 0;  i < n; i++) { list.add(scanner.nextInt()); } int result = 0; while(list.size() > 1){ int a = list.poll(); int b = list.poll(); int temp = a+b; list.add(temp); result += temp; } System.out.println(result); } }

Compare odd and even numbers

Topic link

https://www.nowcoder.com/practice/188472f474d5421cb8218b8ad561023b?tpId=67&tqId=29636&tPage=1&ru=%2Fkaoyan%2Fretest%2F1005&qru=%2Fta%2Fbupt -kaoyan%2Fquestion-ranking

Analysis of ideas

This is too simple. Just input and count

AC code

 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int odd = 0; int even = 0; for (int i = 0;  i < n; i++) { if (scanner.nextInt() % 2 == 0) { even ++; }else{ odd ++; } } if (even > odd) { System.out.println("NO"); }else { System.out.println("YES"); } } }

Find the kth decimal

Topic link

https://www.nowcoder.com/practice/204dfa6fcbc8478f993d23f693189ffd?tpId=67&tqId=29637&tPage=1&ru=%2Fkaoyan%2Fretest%2F1005&qru=%2Fta%2Fbupt -kaoyan%2Fquestion-ranking

Analysis of ideas

I like the sort method of list in Java too much. It's very convenient.

Therefore, I first de duplicate the list, and then sort it. Then I can directly output the k-1 number

AC code

 import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); List<Integer>list = new ArrayList<>(); for (int i = 0;  i < n; i++) { list.add(scanner.nextInt()); } int k = scanner.nextInt(); list = new ArrayList<>(new LinkedHashSet<>(list)); Collections.sort(list); System.out.println(list.get(k-1)); } }

Matrix power

Topic link

https://www.nowcoder.com/practice/31e539ab08f949a8bece2a7503e9319a?tpId=67&tqId=29638&tPage=1&ru=%2Fkaoyan%2Fretest%2F1005&qru=%2Fta%2Fbupt -kaoyan%2Fquestion-ranking

Analysis of ideas

The most important thing in this question is to realize the multiplication of two matrices.

An element of the multiplied matrix result[i][j] Is row i of matrix A a[i][?] And column j of matrix B b[?] [j] Corresponds to the result of multiplication.

AC code

 import java.util.Scanner; public class Main { public static int size = 0; public static void main(String[] args) { Main mutiple = new Main(); mutiple.handle(); }     public void handle(){ Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); size = n; int k = scanner.nextInt(); int [][] array = new int[n][n]; for (int i = 0;  i < n; i++) { for (int j = 0;  j < n; j++) { array[i][j] = scanner.nextInt(); } } int [][] origin = array; for (int i = 1;  i < k; i++) { array = cheng(array, origin); } for (int i = 0;  i < n; i++) { for (int j = 0;  j < n; j++) { System.out.print(array[i][j]); if (j!= n-1) { System.out.print(" "); } } System.out.println(); } } public int[][] cheng(int [][] arrayA,int [][]arrayB){ int[][] result = new int[size][size]; for (int i = 0;  i < size; i++) { for (int j = 0;  j < size; j++) { int temp = 0; for (int l = 0;  l < size; l++) { temp += arrayA[i][l] * arrayB[l][j];     } result[i][j] = temp; } } return result; } }

C Flip

Topic link

https://www.nowcoder.com/practice/74bdb725421c4f80b4aca7266818baf0?tpId=67&tqId=29639&tPage=1&ru=%2Fkaoyan%2Fretest%2F1005&qru=%2Fta%2Fbupt -kaoyan%2Fquestion-ranking

Analysis of ideas

stay Beijing Post 2014 Planning Institute Machine Test (Morning) Question B: Rotating an image is similar to this:

It is to find the corresponding relationship between the matrix rotated by 90 ° and the original matrix.

For example, if you rotate 90 ° clockwise, the rotated matrix will be output in columns as before, and will be output according to the last digit.

For example, the following matrix:

Rotate 90 ° clockwise to output from the last digit in the first column, 7, 4, 1/8, 5, 2/9, 6 and 3 in turn.

If the number 1 is (x-1, y-1), the position of the number 7 is (x-1+b-1, y-1), (where b=3)

The first line is (x-1+b-1 -?, y-1+0)

The second line is (x-1+b-1 -?, y-1+1)

The third line is (x-1+b-1 -?, y-1+2)

? It indicates the position of this element in this line, and it is the No ?+ one Element. In code ? = j - y + 1

The second coordinate rule of each row of the rotated matrix is y-1+(i-x+1)

AC code

 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[][] array = new int[5][5];  for(int i = 0;  i <5;i++){ for (int j = 0;  j < 5; j++) { array[i][j] = scanner.nextInt(); } } int a = scanner.nextInt(); int b = scanner.nextInt(); String oper = a+""+b; int x = scanner.nextInt(); int y = scanner.nextInt(); int [][] newArray = new int[5][5]; switch (a) { case 1: for (int i = x-1;  i < x + b -1; i++) { for (int j = y -1;  j < y + b -1; j++) { newArray[i][j] = array[x+b+y-j-3][y+i-x];     } } break; case 2: for (int i = x-1;  i < x + b -1; i++) { for (int j = y -1;  j < y + b -1; j++) { newArray[i][j] = array[j- y + x][x+y+b-i-3];     } } break; default: break; } for(int i = 0;  i <5;i++){ for (int j = 0;  j < 5; j++) { if (i>= x-1 && i < x + b -1 && j>=y-1 && j < y + b -1) { System.out.print(newArray[i][j]); }else { System.out.print(array[i][j]); } if (j !=  4) { System.out.print(" "); } } System.out.println(); } } }

play cards

Topic link

https://www.nowcoder.com/practice/82442ee76977479e8ab4b88dfadfca9f?tpId=67&tqId=29640&tPage=1&ru=%2Fkaoyan%2Fretest%2F1005&qru=%2Fta%2Fbupt -kaoyan%2Fquestion-ranking

Analysis of ideas

This question has been simplified, and the data given must be sorted (with the sorted cards in hand). So when making a judgment, we use a loop, in which the number of cards in the loop is larger than that given by the question, and then use Java's String.contains Judge whether the card in your hand has a constructed card.

AC code

 import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedHashSet; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String string = scanner.nextLine(); String string2 = scanner.nextLine(); char[] array = string2.toCharArray(); int lenght = array.length; boolean isHave = false; switch (lenght) { case 1: int value = Integer.parseInt(string2); char[] arrayByhave  = string.toCharArray(); for (int i = 0;  i < arrayByhave.length; i++) { if (Integer.parseInt(arrayByhave[i] + "") > value ) { isHave = true; break; } } break; case 2: case 3: case 4: //            System.out.println("ok"); int start = Integer.parseInt(array[0] + ""); for (int i = start + 1;  i <= 9; i++) { String temp = ""; for (int j = 0;  j < lenght; j++) { temp += i + "";  } //System. out. println ("temporarily generated string"+temp); if (string.contains(temp)) { isHave = true; break; } } break; case 5: String[]  strArray = new String[]{"12345", "23456", "34567","45678","56789"};  int flag = 0; for (int i = 0;  i < strArray.length; i++) { if (string2.equals(strArray[i])) { flag = i; break; } } String[] str1Array = string.split(""); List<String> list = new ArrayList<>(new LinkedHashSet<>(Arrays.asList(str1Array))); StringBuilder sb = new StringBuilder();   for (int i = 0;  i < list.size();  i++) {   sb.append(list.get(i));   } String listToString = sb.toString(); //System. out. println ("string"+listToString); for (int i = flag + 1;  i < strArray.length; i++) { if (listToString.contains(strArray[i])) { isHave = true; break; } } break; default: break; } if (isHave) { System.out.println("YES"); }else { System.out.println("NO"); } } }

Tree lookup

Topic link

https://www.nowcoder.com/practice/9a10d5e7d99c45e2a462644d46c428e4?tpId=67&tqId=29641&tPage=1&ru=%2Fkaoyan%2Fretest%2F1005&qru=%2Fta%2Fbupt -kaoyan%2Fquestion-ranking

Analysis of ideas

Although this question is a tree question, it is unnecessary to construct a real tree, because the question says yes Complete binary tree , output all nodes of a certain depth. All node numbers at a certain depth k have a range, starting with 2 ^ (k-1) and ending with min (total number of nodes - 1, 2 ^ k-1).

If the start number is greater than the end code, there is no node of this depth.

AC code

 import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); List<Integer> list = new ArrayList<>(); for (int i = 0;  i < n; i++) { list.add(scanner.nextInt()); } int depth = scanner.nextInt(); int vertexNumStart = 0; int vertexNumEnd = 0; for (int i = 1;  i < depth; i++) { vertexNumStart += vertexsInDepth(i); } vertexNumEnd = vertexNumStart + vertexsInDepth(depth); int end = Math.min(list.size(), vertexNumEnd); if (end <= vertexNumStart) { System.out.println("EMPTY"); }else { for (int i = vertexNumStart;  i < end; i++) { System.out.print(list.get(i)); if (i!= end -1) { System.out.print(" "); } }         } } public static int vertexsInDepth(int depth) { return (int) Math.pow(2, depth -1); } }

lookup

Topic link

https://www.nowcoder.com/practice/a988eda518f242c29009f8620f654ede?tpId=67&tqId=29642&tPage=1&ru=%2Fkaoyan%2Fretest%2F1005&qru=%2Fta%2Fbupt -kaoyan%2Fquestion-ranking

Analysis of ideas

The title is search. In fact, the content is the operation of string.

One is to flip some substrings of a string, and the other is to replace some substrings.

This uses the java StringBuilder Built in reverse() and replace The two methods are simple.

AC code

 import java.util.Scanner; public class Main { static Scanner scanner; static String string; public static void main(String[] args) { scanner = new Scanner(System.in); string = scanner.next(); int num = scanner.nextInt(); for (int i = 0;  i < num; i++) { Main search = new Main(); search.handle();     } } public void handle(){ String opration = scanner.next(); char[] toArray = opration.toCharArray(); switch (toArray[0]) { case '0': int start = Integer.parseInt(toArray[1] + ""); int end = Integer.parseInt(toArray[1] + "") + Integer.parseInt(toArray[2] + ""); String subString = string.substring(start,end); subString = new StringBuilder(subString).reverse().toString(); string = new StringBuilder(string).replace(start, end, subString).toString(); break; case '1': int start1 = Integer.parseInt(toArray[1] + ""); int end1 = Integer.parseInt(toArray[1] + "") + Integer.parseInt(toArray[2] + ""); StringBuilder     ToReplaceStrSb = new StringBuilder(); for (int i = 3;  i < toArray.length; i++) { ToReplaceStrSb.append(toArray[i]); } String toReplaceStr = ToReplaceStrSb.toString(); string = new StringBuilder(string).replace(start1, end1, toReplaceStr).toString(); break; default: break; } System.out.println(string); } }

Complex set

Topic link

https://www.nowcoder.com/practice/abdd24fa839c414a9b83aa9c4ecd05cc?tpId=67&tqId=29643&tPage=1&ru=%2Fkaoyan%2Fretest%2F1005&qru=%2Fta%2Fbupt -kaoyan%2Fquestion-ranking

Analysis of ideas

This problem is very simple. It is to create a plural class, and then add and delete plural. There is nothing to say.

The difficulty is to process the input.

AC code

 import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Scanner; public class Main { List<Fu> fus = new ArrayList<>(); static Scanner scanner; public static void main(String[] args) { scanner = new Scanner(System.in); Main fushu = new Main(); fushu.handle(); } public void  handle() { fus.clear(); int num = scanner.nextInt(); scanner.nextLine(); for (int i = 0;  i < num; i++) { String oper = scanner.nextLine(); String[] operArray = oper.split(" "); switch (operArray[0]) { case "Pop": Pop(); break; case "Insert": Insert(operArray[1]); break; default: break; } } } public void Pop(){ if (fus.size() == 0) { System.out.println("empty"); }else { Collections.sort(fus,new Comparator<Fu>() { @Override public int compare(Fu o1, Fu o2) { if (o1.getMoValue() < o2.getMoValue()){ return 1; }else if (o1.getMoValue() > o2.getMoValue()) { return -1; }else { if (o1.xu > o2.xu) { return 1; }else { return -1; } } } });     System.out.println(fus.get(0).shi + "+i" + fus.get(0).xu); fus.remove(0); System.out.println("SIZE = " + fus.size()); } } public void Insert(String data){ String[] array = data.split("\\+"); fus.add(new Fu(Integer.parseInt(array[0]),getRealXu(array[1]))); System.out.println("SIZE = " + fus.size()); } public int getRealXu(String str){ return Integer.parseInt(str.substring(1)); }; class Fu{ int shi; int xu; public int getMoValue(){ return (int) (Math.pow(shi, 2) + Math.pow(xu, 2));         } public Fu() { } public Fu(int shi, int xu) { super(); this.shi = shi; this.xu = xu; } } }

Binary sort tree

Topic link

https://www.nowcoder.com/practice/b42cfd38923c4b72bde19b795e78bcb3?tpId=67&tqId=29644&tPage=1&ru=%2Fkaoyan%2Fretest%2F1005&qru=%2Fta%2Fbupt -kaoyan%2Fquestion-ranking

Analysis of ideas

This question is a little difficult.

It is a recursive process to construct a binary sort tree first. Keep looking for the insertion location, and then insert the node to the appropriate location.

But for the language of Java, function parameter transfer can only transfer values, not pointers or references, so we cannot use recursion as written in textbooks.

The termination condition of recursion in textbooks is bt == null , and our termination condition is bt.right == null || bt.left == null , because all the functions in the textbook are Transmitted reference In, so bt == null When the time comes, give it to the back bt Assign a new node, and the new node will be automatically linked to its parent node! But what we pass is that if the value is past, the new node must be linked manually.

The structure of the binary tree in this question is to use the left and right child structure.

AC code

 import java.util.Scanner; public class Main { static Scanner scanner; TreeNode root; public static void main(String[] args) { scanner = new Scanner(System.in); while (scanner.hasNext()) { Main binarySortingTree = new Main(); binarySortingTree.handle();     } } public void handle() { int num = scanner.nextInt(); int value = scanner.nextInt(); root = new TreeNode(value); for (int i = 1;  i < num; i++) { int temp = scanner.nextInt(); BSTInsert(root,temp); } preoder(root); System.out.println(); inorder(root); System.out.println(); postOrder(root); System.out.println(); } public void preoder(TreeNode p) { if (p !=  null) { System.out.print(p.value + " "); preoder(p.left); preoder(p.right); } } public void inorder(TreeNode p){ if (p !=  null) { inorder(p.left); System.out.print(p.value + " "); inorder(p.right); } } public void postOrder(TreeNode p){ if (p !=  null) { postOrder(p.left); postOrder(p.right); System.out.print(p.value + " "); } } public void BSTInsert(TreeNode bt, int value){ if (value !=  bt.value) { if (value > bt.value) { if (bt.right == null) { bt.right = new TreeNode(value); }else{ BSTInsert(bt.right, value); } }else if (value < bt.value) { if (bt.left == null) { bt.left = new TreeNode(value); }else{ BSTInsert(bt.left, value); } } } } class TreeNode{ int value; TreeNode left; TreeNode right; public TreeNode(int value) { this.value = value; this.left = null; this.right = null; } public TreeNode() { } } }

Find the Decimal

Topic link

https://www.nowcoder.com/practice/ba91786c4759403992896d859e87a6cd?tpId=67&tqId=29645&tPage=1&ru=%2Fkaoyan%2Fretest%2F1005&qru=%2Fta%2Fbupt -kaoyan%2Fquestion-ranking

Analysis of ideas

This question is very simple. It is still a pair of numbers, but the Map cannot be used. Because the key of the Map cannot be repeated, it does not match the entry of the question. Create a class by yourself. Then use the sort method of list to sort.

AC code

 import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; public class Main { public static void main(String[] args) { Main minPair = new Main(); minPair.handle(); } public void handle(){ Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); List<Pair> list = new ArrayList<>(); for (int i = 0;  i < n; i++) { Pair temp = new Pair(scanner.nextInt(), scanner.nextInt()); list.add(temp); } Collections.sort(list,new Comparator<Pair>() { @Override public int compare(Pair o1, Pair o2) { if (o1.getKey() > o2.getKey()) { return 1; }else if (o1.getKey() < o2.getKey()) { return -1; }else { return o1.getValue().compareTo(o2.getValue()); } } }); System.out.println(list.get(0).getKey() + " " + list.get(0).getValue()); } class Pair{ int x; int y; Pair(int x, int y) { super(); this.x = x; this.y = y; } public Integer getKey(){ return x; } public Integer getValue(){ return y; } @Override public String toString() { //Method stub automatically generated by TODO return "x=" + x + "y=" + y; } } }

lookup

Topic link

https://www.nowcoder.com/practice/d93db01c2ee44e8a9237d63842aca8aa?tpId=67&tqId=29646&tPage=1&ru=%2Fkaoyan%2Fretest%2F1005&qru=%2Fta%2Fbupt -kaoyan%2Fquestion-ranking

AC code

 import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); List<Integer> list = new ArrayList<>(); for (int i = 0;  i < n; i++) { list.add(scanner.nextInt()); } int m = scanner.nextInt(); for (int i = 0;  i < m; i++) { int temp = scanner.nextInt(); if (list.contains(temp)) { System.out.println("YES"); }else { System.out.println("NO"); } } } } }
Last modification: March 23, 2019
Do you like my article?
Don't forget to praise or appreciate, let me know that you accompany me on the way of creation.