- Tao JAVA BASIC Programming Exercises
"Program 1"
Title: Classical Questions: There are a pair of rabbits, from the 3rd month after birth a pair of rabbits each month, the small rabbit to the third month after the birth of a pair of rabbits each month, if the rabbit is not dead, ask the number of rabbits each month?
Program Analysis: The law of Rabbits for the series 1,1,2,3,5,8,13,21 ....
| public class prog1{ public static void Main (string[] args) { int n = ten; System.out.println ("+n+" Each month the total number of rabbits is "+fun (n)"); } private static int fun (int n) { if (n==1 | | n==2) return 1; Else return Fun (n-1) + Fun (n-2); } } |
"Program 2"
Title: Determine the number of primes between 101-200 and the output of all primes.
Program Analysis: The method of judging primes: to remove 2 to sqrt (this number) with a number, if divisible, indicates that the number is not a prime, and vice versa is a prime.
public class prog2{ public static void Main (string[] args) { int m = 1; int n = 1000; int count = 0; Number of statistical primes for (int i=m;i<n;i++) { if (IsPrime (i)) { count++; System.out.print (i+ ""); if (count%10==0) { System.out.println (); } } } System.out.println (); System.out.println ("+count+" in "+m+" and "+n+"); } Judging prime numbers private static Boolean isprime (int n) { Boolean flag = true; if (n==1) Flag = false; else{ for (int i=2;i<=math.sqrt (n); i++) { if ((n%i) ==0 | | n==1) { Flag = false; Break } Else Flag = true; } } return flag; } } |
···
"Program 3"
Title: Print out all the "daffodils", the so-called "Narcissus number" refers to a three-digit number, its number of cubes and equal to the number itself. For example: 153 is a "narcissus number", because the 153=1 three times the square +5 of the three +3 Times Square.
Program Analysis: Use for loop control 100-999 number, each number decomposition out of bits, 10 bits, hundred.
public class prog3{ public static void Main (string[] args) { for (int i=100;i<1000;i++) { if (Islotus (i)) System.out.print (i+ ""); } System.out.println (); } Judging the number of daffodils private static Boolean islotus (int lotus) { int m = 0; int n = Lotus; int sum = 0; m = n/100; N-= m*100; sum = m*m*m; m = N/10; N-= m*10; Sum + = m*m*m + n*n*n; if (sum==lotus) return true; Else return false; } } |
Java BASIC Programming exercises