First, describe
Topic 1: Determine whether a number is a 2 order square, for example, 8,16,64,256 are 2 of the order of the number.
Problem Analysis: If a number is the order number of 2, then the number of binary number of the first is 1, followed by a number of 0, for example, 8 with a binary representation of 1000,64 1000000, if the number is reduced by 1, and this number to do a bitwise & operation of 0, that is (number-1) &number==0,8&7=1000&0111=0000.
Topic 2: Lists the combinations of all elements in an array, such as 1, 2, and 3 listed as 1, 12, 123, 13, 132, 2, 21, 213, 23, 231, 3, 31, 312, 32, 321, using recursive methods
Topic 3: Find the letter with the most occurrences in a string and the number of occurrences, and if more than one character appears, the number of characters is listed.
problem parsing: Converting a string to a char array, and then placing each character in TreeSet and ArrayList, because the TreeSet itself has a sort function, added to the default sequence of char, and this collection cannot have duplicate elements, So the same character can be stored only once, and the duplicate char is stored in ArrayList. We use the Collections.sort () function to sort the characters in the ArrayList, so the same characters must be contiguous. It then loops through the elements in the TreeSet collection, looking for the first and last occurrence of the element in ArrayList, the number of occurrences = The last occurrence-the number of occurrences of the first occurrence of +1.
Second, the source code
Program 1:
<span style= "FONT-SIZE:18PX;" >package Tong.yue.socket;import Java.util.scanner;public class Ispower {/** * Determines whether a number is a 2 order square, for example, 8, 16,64,256 is the order number of the 2 * Resolution: If a number is the order of 2, then the number of binary number of the first is 1, followed by a number of 0, for example, 8 with a binary representation of 1000,64 1000000, * If this number minus 1, and this number to do a bitwise & The operation is 0, i.e. (number-1) &number==0 * @param args */public static void main (string[] args) {Scanner Scanner = new Scanner (Sys tem.in); SYSTEM.OUT.PRINTLN ("Please input a number between 1-50000:"), int number = Scanner.nextint (), while (true) {if (number<= 0| | number>50000) {System.out.println ("number error,please input a number between 1-50000 again:"); Scanner.nextint ();} else {break;}} Boolean flag = Ispowernumber (number), if (flag) {System.out.println (number+) is the order square of 2. ");} else {System.out.println (number+) is not the order number of 2. ");}} According to the characteristics of the binary data divisible by 2, preceded by a 1 followed by a number of 0, such as: 1000 means that the decimal 8 can be divisible by 2, the 8-1=7,7 binary is represented as 0111, and the two phases are all 0private static Boolean Ispowernumber (int number) {if (Number!=0 && (number& (number-1)) ==0) {return true;} return false;}} </span>
Operation Result:
Program 2:
<span style= "FONT-SIZE:18PX;" >package tong.yue.recursive;import java.util.arrays;import Java.util.linkedlist;import java.util.List;public Class Arraycombination {/** * lists a combination of all elements in an array, such as 1, 2, 3 columns out of 1, 12, 123, 13, 132, 2, 21, 213, 23, 231, 3, 31, 312, 32, 321 * @param args */public static void Main (string[] args) {string[] strings =new string[]{"2", "3", "4", "5"};listall (Arrays.aslist ( strings), "");} Use a recursive method to list all the elements in an array all combinations of private static void Listall (List<string> aslist, string string) {System.out.println ( string); for (int i = 0; i < aslist.size (); i++) {list<string> templist = new linkedlist<string> (aslist); Lis TAll (Templist, String+templist.remove (i));}}} </span>
Operation Result:
Program 3:
<span style= "FONT-SIZE:18PX;" >package Tong.yue.stringcountchar;import Java.beans.beancontext.beancontext;import Java.io.PrintWriter;import Java.util.arraylist;import Java.util.collections;import Java.util.iterator;import Java.util.Scanner;import Java.util.treeset;public class Charcountrank {/** * Find the letter with the most occurrences in a string and the number of occurrences, and if more than one character appears, the number of characters is listed. * A string may contain multiple characters in a-Z and can be listed if there are duplicate characters, such as string= "AAJSJAOTTTTTJDADJCJIAF". * Topic parsing: Converting a string to a char array, and then placing each character in TreeSet and ArrayList, because TreeSet itself has a sort function, added to the default sequence of char, and this collection cannot have duplicate elements, so the same character can only be stored once , and a duplicate char can be stored in the ArrayList. * We use the Collections.sort () function to sort the characters in the ArrayList, so the same characters must be contiguous. * then loop through the elements in the TreeSet collection to find the first and last occurrence of the element in ArrayList, the number of occurrences = Last occurrence-Number of occurrences +1 * The most frequently occurring characters are stored in another ArrayList, and record the maximum number of times the character appears, and then print out the output. * * @param args */public static void main (string[] args) {Scanner Scanner = new Scanner (system.in); System.out.println ("Please enter an English string:"); String string = Scanner.nextline (); Statisticscharrank (string);} /** * Converts a string to a char array, and then places each character in TreeSet and ArrayList. * Because TreeSet itself has a sort function, added to the default sequence of char, and this collection can not have duplicate elements, so the same character can only be stored once, and ArrayList can store the duplicate char. * We use the Collections.sort () function to sort the characters in the ArrayList, so the same characters must be contiguous. * then loop through the elements in the TreeSet collection to find the first and last occurrence of the element in ArrayList, the number of occurrences = Last occurrence-Number of occurrences +1 * The most frequently occurring characters are stored in another ArrayList, and record the maximum number of times the character appears, and then print out the output. * For example: * Treeset:[a, D, F, I, J, O, A, D, H, I, O] sorted arraylist:[a, D, D, D, D, D, F, I, I, I, I, I, I, I, I, I, I, J, O, O , A, a, a, D, D, D, H, I, I, I, O, O, O] ArrayList of the same character in the adjacent * @param string */private static void Statisticscharrank (Stri ng string) {char[] chars = String.tochararray (); TreeSet TreeSet = new TreeSet (); ArrayList ArrayList = new ArrayList (); for (int i = 0; i < chars.length; i++) {Treeset.add (string.valueof (chars[i])); arr Aylist.add (string.valueof (Chars[i));} System.out.println ("TreeSet:" +treeset); Collections.sort (arrayList); System.out.println ("ArrayList after sorting:" +arraylist); String inputstring = arraylist.tostring ();//Use a arrayList to hold the maximum number of characters arrayList Arraylistmax = new arrayList (); int max = 0; String maxstring= "";//Loop through TreeSet to determine the number of occurrences of each character in the first and last occurrences of the ArrayList iterator iterator = Treeset.iterator (); while (Iterator.hasnext ()) {String object = (string) iterator.next ();//The position of the first occurrence of the character int beginindex = Arraylist.indexof ( object);//The position of the last occurrence of the character int lastIndex = Arraylist.lastindexof (object);//The number of occurrences of this character is counted int value = lastindex-beginindex+1; if (max < value) {//appears with the most new characters, emptying the element previously recorded in Arraylistmax, adding the current element max = Value;maxstring = Object;arraylistmax.clear () ; Arraylistmax.add (object);} else if (max==value) {//If different characters appear the same number of times, add this element to Arraylistmaxarraylistmax.add (object);}} System.out.print ("the most likely characters are:"); System.out.print (Arraylistmax); System.out.println ("\ nthe number of occurrences:" +max);}} </span>
Operation Result:
Java BASIC Programming 50 questions (25-27 questions) detailed