Some of the classic algorithms in Java (i) "program 01" title: Classical Problems: There are a pair of rabbits, from the first 3 months after birth of a pair of rabbits each month, the rabbit long to the fourth month after the birth of a pair of rabbits every month, if the rabbit is not What is the total number of rabbits per month?
Analysis: The first month rabbit logarithm is 1, the second month rabbit logarithm is 1, the third month the Rabbit logarithm is 2, the fourth month Rabbit logarithm is 3, the fifth month Rabbit logarithm is 5, ....
The "Rabbit Sequence" (also known as the Fibonacci sequence), starting from the second, each item equals the sum of the first two, 1,1,2,3,5,8,13,21,34,.... It is important to note that the first 1 represents the 1th item and the No. 0 item is 0.
The following Java code only outputs the rabbit logarithm of the month, based on the input month.
Import Java.util.scanner;public class Java01 {public static void main (string[] args) {Scanner sc = new Scanner (system.in); System.out.print ("Please enter the month to query:"); int month = Sc.nextint (); System.out.println ("the" + month + "Rabbit Total" + fib (month) + "pair. ");} public static int fib (int month) {if (month = = 1 | | month = = 2) {return 1;} else {return fib (month-1) + fib (month-2);} }}
To run the example:
"program 02" title: Determine the number of primes between 101-200 and output all primes.
Analysis: The prime number, which can be expressed as a product of itself and 1, cannot be expressed as the product of any other two integers. one way to determine the prime number is to remove the square root of 2 to this number, if divisible, to indicate that the number is not a prime and vice versa.
Import Java.util.arraylist;public class Java02 {public static void main (string[] args) {int m, N;int temp = 0; Arraylist<integer> alist = new arraylist<integer> (); A:for (n = 101; n <=; n++) {//adopts the form of a label for (M = 2; m < N/2; m++) {if (n% m = = 0) {continue A;}} Alist.add (n);} System.out.println ("101-200 Total" +alist.size () + "number of primes. "); System.out.println ("respectively:"); for (int one:alist) {temp++; System.out.print (one+ ""); if (temp% 5 = = 0) { //Line output 5 System.out.println ();}}}
Operation Result:
"program 03" title: Number of primes within 100analysis: And the idea of program 02 is broadly consistent.
public class Java03 {public static void main (string[] args) {int m, N;int temp = 0; System.out.println ("The prime number within 100 is:"); A:for (n = 2; n <=; n++) {//takes the form of a label for (M = 2; m < N/2; m++) {if (n% m = = 0) {continue A;}} System.out.print (n + ""); temp++;if (temp% 5 = = 0) {//Line output 5 System.out.println ();}}}
Output Result:
"program 04" title: There are 1, 2, 3, 4 digits, how many can be composed of different and no repetition of the number of three digits? How much are they?
import Java.util.arraylist;public class Java04 {public static void main (string[] args) {arraylist<integer> alist = new Arraylist<integer> (); int temp = 0; int count = 0; for (int i = 1, i <= 4; i++) {for (int j = 1; J <= 4; j + +) {for (int k = 1; k <= 4; k++) {if (i! = J && J! = k && k! = i) {temp = i * + + j * Ten + K; Alist.add (temp); }}}} System.out.println ("Can Make Up" +alist.size () + "a three-digit number that is not identical and has no duplicates. "); System.out.println ("respectively:"); for (int one:alist) {count++; System.out.print (one+ ""); if (count% 5 = = 0) {//Line output 5 System.out.println ();}}}
Output Result:
"program 05" title: Decompose a positive integer into factorization. For example: Enter 90 and print out 90=2*3*3*5.
Import Java.util.scanner;public class Java05 {public static void main (string[] args) {Scanner sc = new Scanner (system.in); System.out.print ("Please enter a positive integer:"); int n = sc.nextint (); The results of System.out.println (n+ "fractional solution are as follows:"); System.out.print (n + "="); solution (n);} public static void solution (int n) {for (int i = 2; I <= n; i++) {if (n = = i) {System.out.println (i); return;} if (n > I && (n% i = = 0)) {System.out.print (i + "*"); solution (n/i); break;}}}
Input Example:
"program 06" title: Enter a line of characters, respectively, the number of English letters, spaces, numbers and other characters.
Import Java.util.scanner;public class Java06 {public static void main (string[] args) {System.out.println ("Please enter a line of characters:"); Scanner sc = new Scanner (system.in); String inStr = Sc.nextline (); SYSTEM.OUT.PRINTLN ("Statistical results are as follows:"); count (INSTR);} public static void Count (String inStr) {int letter = 0;int Number = 0;int Blank = 0;int other = 0;for (int i = 0; i < i Nstr.length (); i++) {Char in = Instr.charat (i), if (in >= ' 0 ' && "<= ' 9 ') Number++;else if (in >= ' a ' & in <= ' Z ') || (In >= ' A ' & in <= ' Z ')) Letter++;else if (in = = ") blank++;elseother++;} System.out.println ("Number of letters:" + letter + "\ n number of numbers:" + number+ "\ n the number of spaces:" + blank + "\ n the number of other symbols:" + other);}}
Input Example:
"program 07" title: Monkeys Eat Peach problem: The first day of monkeys to take off a number of peaches, immediately ate half, not addicted, and ate a second morning and the rest of the peach eaten half,and ate one more. Every morning after eating the rest of the day half of the zero one. When I want to eat again in the morning of the 10th day, I see only one peach left. For the first day to pick a total number.
Analysis: Assuming that the number of monkeys to eat peach before the sum, the number of peaches to eat is sum/2+1, the remaining amount of daily peaches is: res = sum-(sum/2+1), that is, sum = (res+1) * *. According to test instructions, the 10th day of the peach sum of 1, can be understood as the 9th day of the Res 1, so that the 9th day of the sum= (*2=4), the sum of 9th days is also the 8th day res ... In this way, we can draw a total of how many peaches were picked on the first day.
public class Java07 {public static void main (string[] args) {int sum = 0;int res = 1;for (int day = 9; day >= 1; day--) {sum = (res + 1) * 2;res = sum;} System.out.println ("The first day of the total pick" + sum + "a peach. ");}}
Operation Result:
Java Classic Algorithm (i)