About Java Classic Algorithm 40 (Super practical version) _java

Source: Internet
Author: User
Tags arrays case statement gcd lowercase pow readline
"Program 1" topic: Classical question: There are a pair of rabbits, from the 3rd month after the birth of a pair of rabbits every month, the rabbit long to the fourth months after the birth of a pair of rabbits, if the rabbit is not


Death, ask the number of rabbits per month for how many?


1. Procedure analysis: Rabbit's rule for series 1,1,2,3,5,8,13,21 ....


public class exp2{public static void Main (String args[]) {int i=0;


System.out.println (f (i));


}public static int f (int x)


{if (X==1 | | x==2) return 1; else


return f (x-1) +f (x-2);


}


}


Or


public class exp2{public static void Main (String args[]) {int i=0;


Math MyMath = new Math (); for (i=1;i<=20;i++)


System.out.println (MYMATH.F (i));


}


}class Math


{public int f (int x)


{if (X==1 | | x==2) return 1; else


return f (x-1) +f (x-2);


}


}


"Program 2" topic: Determine how many primes are between 101-200, and output all prime numbers.


1. Procedure analysis: The method of determining prime numbers: To remove 2 to sqrt (this number) separately, if divisible by a number,


It indicates that the number is not prime, whereas it is prime.


public class exp2{public static void Main (String args[]) {int i=0;


Math MyMath = new Math (); for (i=2;i<=200;i++) if (Mymath.iszhishu (i) ==true)


System.out.println (i);


}


}class Math


{public int f (int x)


{if (X==1 | | x==2) return 1; else


return f (x-1) +f (x-2);


}public boolean Iszhishu (int x)


{for (int i=2;i<=x/2;i++) if (x% 2==0) return false;


}


}


"Program 3" topic: Print out all the "Narcissus number", the so-called "Narcissus number" refers to a three-digit number, its members of the digital cubic and equal to the number itself. For example: 153 is a "narcissus


Number ", because the 153=1 of the three-second party +5 of three times +3 of the three-time party.


1. Program Analysis: Use for loop control 100-999 number, each number decomposition out of single-digit, 10, hundred.


public class exp2{


public static void Main (String args[]) {int i=0;


Math MyMath = new Math (); for (i=100;i<=999;i++) if (Mymath.shuixianhua (i) ==true)


System.out.println (i);


}


}class Math


{public int f (int x)


{if (X==1 | | x==2) return 1; else


return f (x-1) +f (x-2);


}public boolean Iszhishu (int x)


{for (int i=2;i<=x/2;i++) if (x% 2==0) return false;


}public boolean Shuixianhua (int x)


{int i=0,j=0,k=0;


i=x/100;


j= (x 100)/10;


Ik=fx% 10; (x==i*i*i+j*j*j+k*k*k) return true; Else


return false;


}


}


"Program 4" topic: Decompose a positive integer into decomposition. For example: Enter 90, print out 90=2*3*3*5.


Program analysis: N to decompose decomposition, you should find a minimum prime number k, and then complete the following steps:


(1) If this prime number is equal to n, then the process of decomposing decomposition is finished and printed out.


(2) if n <> K, but n can be divided by K, you should print out the value of K, and with n divided by the quotient of K, as a new positive integer you, repeat the first step.


(3) If n cannot be divisible by K, then repeat the first step using k+1 as the value of K.


public class exp2{Public exp2 () {} public void Fengjie (int. N) {for (int i=2;i<=n/2;i++) {if (n%i==0) {


System.out.print (i+ "*");


Fengjie (n/i);


}


}


System.out.print (n);


System.exit (0);///can not be less this sentence, or the result will be wrong


}public static void Main (string[] args) {


String str= "";


EXP2 c=new exp2 ();


Str=javax.swing.joptionpane.showinputdialog ("Enter the value of N (Enter exit exit):");


int N;


nt=r0y; {


N=integer.parseint (str);


}catch (NumberFormatException e) {


E.printstacktrace ();


}


System.out.print (n+ "decomposition decomposition:" +n+ "=");


C.fengjie (N);


}


}


"Program 5" topic: Use the nesting of conditional operators to complete this problem: Students of academic Grades > = 90 points with a said, 60-89 points between the use of B, 60 points below the use of C.


1. Procedural analysis: (a> b)? A:b This is a basic example of a conditional operator.


Import javax.swing.*; public class Ex5 {public static void main (string[] args) {


String str= "";


Str=joptionpane.showinputdialog ("Enter the value of N (Enter exit exit):");


int N;


nt=r0y; {


N=integer.parseint (str);


Ca}tch (NumberFormatException e) {


E.printstacktrace ();


}


Str= (n>90?) A ":(n>60?" B ":" C "));


System.out.println (str);


}


}


"Program 6" topic: Enter two positive integers m and n, and ask for their gcd and LCM.


1. Procedure analysis: The use of rolling division.


gcd


public class commondivisor{public static void Main (String args[])


{commondivisor (24,32);


}static int commondivisor (int M, int N)


{if (n<0| | M<0)


{


Sryesttuermn.out.println ("error!"); -1;


}if (n==0)


{


RSeytsutrenm.out.println ("The biggest common divisor is:" +m); M


}return commondivisor (n,m%n);


}


}


LCM and GCD:


Import Java.util.Scanner; public class CANDC


{


The following method is to find out the GCD


public static int gcd (int m, int n)


{while (true)


{if ((m = m% n) = = 0) return n; if ((n = n% m) = = 0) return m;


}}


public static void Main (String args[]) throws Exception


{


Get input value


Scanner chin = new Scanner (system.in);


/i/ntint a = Chin.nextint (), B = chin.nextint (); a=23; int b=32; int c = GCD (A, b);


System.out.println ("LCM:" + A * b/c + "\ n gcd:" + C);


}}


"Program 7" topic: Enter a line of characters, each of which counts the number of English letters, spaces, numbers and other characters.


1. Program Analysis: Using the while statement, the condition is that the character entered is not ' \ n '.


Import Java.util.Scanner; public class Ex7 {public static void main (String args[])


{


System.out.println ("Please enter a string:");


Scanner scan=new Scanner (system.in);


String Str=scan.next ();


String e1= "[\u4e00-\u9fa5]";


Sintrting e2= "[A-za-z]"; counth=0; int counte=0; Char[] Arrchar=str.tochararray ();


String[] Arrstr=new string[arrchar.length]; for (int i=0;i<arrchar.length; i++)


{


Arrstr[i]=string.valueof (Arrchar[i]);


}for (String i:arrstr)


{if (i.matches (E1))


{


counth++;


}if (I.matches (E2))


{


counte++;


}


}


System.out.println ("Number of Chinese characters" +counth);


System.out.println ("Number of letters" +counte);


}


}


"Program 8" topic: Find the value of S=A+AA+AAA+AAAA+AA...A, where a is a number. For example 2+22+222+2222+22222 (there are 5 numbers added at this time), several digital phases


Keyboard control is added.


1. Procedure analysis: The key is to calculate the value of each item.


Import java.io.*; public class Sumloop {public static void main (string[] args) throws IOException


{int s=0;


String output= "";


BufferedReader Stadin = new BufferedReader (new InputStreamReader (system.in));


System.out.println ("Please enter a value");


Sfotrring input =stadin.readline (); (int i =1;i<=integer.parseint (input); i++)


{


Ionuttput+=input; A=integer.parseint (output);


S+=a;


}


System.out.println (s);


}


}


Another solution:


Import java.io.*; public class Sumloop {public static void main (string[] args) throws IOException


{int s=0; int n; int t=0;


BufferedReader Stadin = new BufferedReader (new InputStreamReader (system.in));


String input = Stadin.readline ();


Fno=rinteger.parseint (input); (int i=1;i<=n;i++) {


T=t*10+n;


S=s+t;


System.out.println (t);


}


System.out.println (s);


}


}


"Program 9" topic: If a number is exactly equal to the sum of its factors, this number is called the "end of the number." For example 6=1+2+3. program to find out all the numbers within 1000.


public class Wanshu {public static void main (string[] args)


{int s; for (int i=1;i<=1000;i++)


{


fs=o0r (int j=1;j<i;j++) if (i% j==0)


if s=s+j; (s==i)


System.out.print (i+ "");


}


System.out.println ();


}


}


"Program 10" topic: a ball from 100 meters height free fall, each landing after the return to the original height of half, and then fell, to the 10th time when the landing, the total number of meters? 10th time Rebound


How high?


public class Ex10 {public static void main (string[] args)


{Double s=0 double t=100 for (int i=1;i<=10;i++)


{


s+=t;


T=T/2;


}


System.out.println (s);


System.out.println (t);


}


}


"Program 11" topic: There are 1, 2, 3, 4 digits, can be composed of a number of different and no duplicate number of three digits? How much are they?


1. Procedure analysis: Can fill in the hundred, 10 digits, single-digit digits are 1, 2, 3, 4. Make up all the permutations and then remove the arrangement that does not satisfy the condition.


public class Wanshu {public static void main (string[] args)


{int i=0; int j=0; int k=0; int t=0; for (i=1;i<=4;i++) to (j=1;j<=4;j++) for (k=1;k<=4;k++) if (i!=j && j! =k && i!=k)


{t+=1;


System.out.println (I*100+J*10+K);


}


System.out.println (t);


}


}


"Program 12" topic: bonuses awarded by enterprises are based on the profit percentage. Profit (I) is less than or equal to $100,000, the bonus may be 10%, the profit is higher than 100,000 yuan, less than 200,000 yuan, less than 100,000


The part of the yuan according to 10% commission, higher than 100,000 yuan, Cocoa Commission 7.5%, 200,000 to 400,000, higher than 200,000 yuan portion, can commission 5%; 400,000 to 600,000 when high


In the part of 400,000 yuan, can commission 3%, 600,000 to 1 million, higher than 600,000 yuan portion, can commission 1.5%, more than 1 million yuan, more than 1 million yuan part by 1% Commission, from


Keyboard input Month profit I, the total number of bonuses should be issued?


1. Procedure analysis: Please use the axis to demarcation, positioning. Note that the definition needs to be defined as a growth integral type.


import java. util.*; public class Test {public static void main (String[]args) {


Double sum;//declare the variable to be stored should be bonuses


Scanner input =new Scanner (system.in);//import Scanner


System.out.print ("Enter monthly Profit");


Double lirun=input. Nextdouble ()//input profit from console


if (lirun<=100000) {


sum=lirun*0.1;


}else if (lirun<=200000) {


sum=10000+lirun*0.075;


}else if (lirun<=400000) {


sum=17500+lirun*0.05;


}else if (lirun<=600000) {


sum=lirun*0.03;


}else if (lirun<=1000000) {


sum=lirun*0.015;


} else{


sum=lirun*0.01;


}


System.out.println ("Should be's bonus is" +sum);


}


}


The rest of the code can be perfected by the reader.


"Program 13"


Title: An integer, it plus 100 is a full square, plus 168 is a full square, what is the number?


1. Procedure analysis: In 100,000 to judge, first of the number plus 100 before the root, and then the number plus 268 after the root, if the result of the root to meet the following conditions, that is the result. Please look at the specific


Analysis:


public class Test {public static void main (String[]args) {


Long k=0; for (k=1;k<=100000l;k++) if (Math.floor (Math.sqrt (k+100)) ==math.sqrt (k+100) &&


Math.floor (Math.sqrt (k+168)) ==math.sqrt (k+168))


System.out.println (k);


}


}


"Program 14" topic: Enter a certain date of the year, Judge this day is the first days of the year?


1. Procedural analysis: Take March 5 as an example, should first add up the first two months, and then add 5 days, the first days of this year, special circumstances, leap year and enter the month greater than 3 o'clock to consider an additional


Days.


Import java.util.*; public class Test {public static void main (String[]args) {int day=0; int month=0; int year=0; int sum=0; int leap;


System.out.print ("Please enter the year, month, day \ \ \ \ \");


Scanner input = new Scanner (system.in);


Year=input.nextint ();


Month=input.nextint ();


Day=input.nextint ();


Switch (month)/* First calculates the total number of days before month of the month * *


{Case 1:


Sum=0;break; Case 2:


Sum=31;break; Case 3:


Sum=59;break; Case 4:


Sum=90;break; Case 5:


Sum=120;break; Case 6:


Sum=151;break; Case 7:


Sum=181;break; Case 8:


Sum=212;break; Case 9:


Sum=243;break; Case 10:


Sum=273;break; Case 11:


Sum=304;break; Case 12:


Sum=334;break; Default


SYSTEM.OUT.PRINTLN ("Data Error");


}


Sum=sum+day; /* Plus days of the day * *


if (year%400==0| | (year%4==0&&year%100!=0)) /* Judgment is leap year * *


Elseleap=1;


Leap=0;


if (leap==1 && month>2)/* If it is a leap year and the month is greater than 2, the total number of days should be plus day * *


sum++;


System.out.println ("It's The" Day: "+sum");


}


}


"Program 15" topic: Enter three integer x,y,z, please put these three numbers from small to large output.


1. Procedure analysis: We find a way to put the smallest number on X, first compare x to Y, if X> y will exchange the value of x and Y, then compare X to Z, if X> z


The x is exchanged with the value of Z, which minimizes the X.


Import java.util.*; public class Test {public static void main (String[]args) {int i=0; int j=0; int k=0; int x=0;


System.out.print ("Please enter three numbers \ n");


Scanner input = new Scanner (system.in);


I=input.nextint ();


J=input.nextint ();


K=iifnput.nextint (); (I>J)


{


X=i;


I=j;


J=x;


}if (i>k)


{


X=i;


I=k;


K=x;


}if (j>k)


{


X=j;


J=k;


K=x;


}


System.out.println (i+ "," +j+ "," +k);


}


}


"Program 16" topic: Output 9*9 formula.


1. Procedure analysis: Branch and column consider, a total of 9 rows 9 columns, I control line, J control column.


public class Jiujiu {public static void main (string[] args)


{int i=0; int j=0; for (i=1;i<=9;i++)


{for (j=1;j<=9;j++)


System.out.print (i+ "*" +j+ "=" +i*j+ "T");


System.out.println ();


}


}}


No repeat product (lower triangle)


public class Jiujiu {public static void main (string[] args)


{int i=0; int j=0; for (i=1;i<=9;i++)


{for (j=1;j<=i;j++)


System.out.print (i+ "*" +j+ "=" +i*j+ "T");


System.out.println ();


}


}}


Upper Triangle


public class Jiujiu {public static void main (string[] args)


{int i=0; int j=0; for (i=1;i<=9;i++)


{for (j=i;j<=9;j++)


System.out.print (i+ "*" +j+ "=" +i*j+ "T");


System.out.println ();


}


}}


"Program 17" topic: Monkey Eat Peach problem: The first day the monkey took off a number of peaches, immediately ate half, still not addicted, and eat a second day the next morning to eat half of the remaining peaches,


and ate one more. Every morning after the last day of the rest of the half zero. On the 10th day of the morning when I wanted to eat again, I saw only one peach left. Ask for the first day of the total number of pick.


1. Procedure analysis: Take the reverse thinking method, infer from the backward forward.


public class Monkey Eat peach {


static int total (int day) {if (day = =) {return 1;


}else{return (Total (day+1) +1) *2;


}


Public s}tatic void Main (string[] args)


{


System.out.println (Total (1));


}}


"Program 18" topic: Two table Tennis teams compete, each out of three people. A team for three a,b,c, Team B for x,y,z three people. The list of the matches has been decided by lottery. Someone asked the team about the name of the game.


Single. A said he does not and X, C said he does not and x,z, please compile a program to find the list of three teams.


1. Procedure analysis: To determine the method of prime number: To remove 2 to sqrt (this number), if divisible, it indicates that this number is not prime, conversely is prime.


Import java.util.ArrayList; public class Pingpang {


PSUTBRLIINCG SAT,ABT,ICC; void Main (string[] args) {


String[] op = {"X", "Y", "Z"};


Arraylist<pingpang> arraylist=new arraylist<pingpang> (); for (int i = 0; i < 3; i++) for (int j = 0; J < 3; J +) for (int k = 0; k < 3; k++) {


Pingpang a=new Pingpang (Op[i],op[j],op[k]); if (!a.a.equals (A.B) &&!a.b.equals (A.C) &&!a.a.equals ("X")


&&!a.c.equals ("x") &&!a.c.equals ("Z")) {


Arraylist.add (a);


}


for} (Object a:arraylist) {


System.out.println (a);


}


}public Pingpang (String A, string B, string c) {super (); this.a = A; this.b = b; this.c = C;


}


@pOubvelircride String toString () {


TODO auto-generated Method Stub


Return "A's opponent is" +a+ "," + "B's opponent is" +b+ "," + "C's opponent is" +c+ ";


}


}


"Program 19" topic: Print out the following pattern (diamond)


*


***


******


********


******


***


*


1. Program Analysis: First, the graph into two parts to see, the first four lines of a law, after three lines of a law, using a double for loop, the first layer of control lines, the second layer of control columns.


Triangle:


public class Startg {public static void main (String [] args)


{int i=0; int j=0; for (i=1;i<=4;i++)


{for (j=1;j<=2*i-1;j++)


System.out.print ("*");


System.out.println ("");


}for (i=4;i>=1;i--)


{for (j=1;j<=2*i-3;j++)


System.out.print ("*");


System.out.println ("");


}


}


}


Rhombic:


public class Startg {public static void main (String [] args)


{int i=0; int j=0;


for (i=1;i<=4;i++)


{for (int k=1; k<=4-i;k++)


System.out for. Print (""); (j=1;j<=2*i-1;j++)


System.out.print ("*");


System.out.println ("");


}for (i=4;i>=1;i--)


{for (int k=1; k<=5-i;k++)


For System.out.print (""); (j=1;j<=2*i-3;j++)


System.out.print ("*");


System.out.println ("");


}


}


}


"Program 20" topic: There is a fractional sequence: 2/1,3/2,5/3,8/5,13/8,21/13 ... find out the sum of the first 20 items of this series.


1. Procedure analysis: Please grasp the change rule of numerator and denominator.


public class Test20 {public static void main (string[] args) {float fm = 1f; float FZ = 1f; float temp; float sum = 0f; f or (int i=0;i<20;i++) {


TEMP = FM;


FM = FZ;


FZ = FZ + temp;


sum + + fz/fm;


SYSTEM.OUT.PRINTLN (sum);


}


SYSTEM.OUT.PRINTLN (sum);


}


}


"Program 21" topic: Seek 1+2!+3!+...+20! and


1. Program Analysis: This program is only the accumulation into a tired multiply.


public class Ex21 {The static long sum = 0; static long FAC = 0; public static void Main (string[] args) {long sum = 0; long FAC = 1; for (int i=1; i<=10; i++) {


FAC = FAC * i;


sum + + FAC;


}


SYSTEM.OUT.PRINTLN (sum);


}}


"Program 22" topic: The use of recursive method to find 5!.


1. Procedure analysis: Recursive formula: fn=fn_1*4!


Import Java.util.Scanner; public class Ex22 {public static void main (string[] args) {


Scanner s = new Scanner (system.in); int n = s.nextint ();


Ex22 TFR = new Ex22 ();


System.out.println (Tfr.recursion (n));


}


public long recursion (int n) {Long value = 0; if (n ==1 | | n = = 0) {


Value = 1;


else if (n > 1) {


Value = n * recursion (n-1);


}return value;


}}


"Program 23" topic: There are 5 people sitting together and asking how old is the five person? He said he was 2 years older than the 4th. Asked the 4th man's age, he said he was 2 years older than the 3rd. Ask a third person, say more than


The 2nd NPC is two years old. Ask the 2nd person, say two years old than the first one. Finally asked the first person, he said is 10 years old. How old is the fifth person, please?


1. Procedure analysis: Using recursive method, recursion is divided into two stages of back-push and recursion. To know the age of the fifth person, you need to know the age of the fourth person, and so on, pushing to the first person (10 years),


Push back.


public class Ex23 {static int getage (int n) {if (n==1) {return 10;


}return 2 + getage (n-1);


}public static void Main (string[] args) {


System.out.println ("The Fifth Age is:" +getage (5));


}


}


"Program 24" topic: Give a positive integer not more than 5 digits, ask: first, ask it is a number of digits, two, reverse order to print out the numbers.


Import Java.util.Scanner; public class Ex24 {public static void main (string[] args) {


Ex24 tn = new Ex24 ();


Scanner s = new Scanner (system.in); Long a = S.nextlong (); if (A < 0 | | a > 100000) {


System.out.println ("Error Input, please run this program Again");


System.exit (0);


}if (a >=0 && a <=9) {


System.out.println (A + "is a number");


System.out.println ("In reverse order output is" + ' \ n ' + a);


else if (a >= && a <= 99) {


System.out.println (A + "is two digits");


SYSTEM.OUT.PRINTLN ("Output is in reverse order");


Tn.converse (a);


else if (a >= && a <= 999) {


System.out.println (A + "is three digits");


SYSTEM.OUT.PRINTLN ("Output is in reverse order");


Tn.converse (a);


else if (a >= 1000 && a <= 9999) {


System.out.println (A + "is four digits");


SYSTEM.OUT.PRINTLN ("Output is in reverse order");


Tn.converse (a);


else if (a >= 10000 && a <= 99999) {


System.out.println (A + "is a five-digit number");


SYSTEM.OUT.PRINTLN ("Output is in reverse order");


Tn.converse (a);


}


}public void Converse (long l) {


String s = long.tostring char (l); [] ch = s.tochararray (); for (int i=ch.length-1; i>=0; i--) {


System.out.print (Ch[i]);


}


}}


"Program 25" topic: a 5-digit number, to determine whether it is a palindrome number. That is 12321 palindrome number, the same as the million digits, 10 bits and thousands of the same.


Import Java.util.Scanner; public class Ex25 {static int[] a = new int[5], static int[] B = new int[5]; public static void Main (string[) args) {Boo Lean is =false;


Scanner s = new Scanner (system.in); Long L = S.nextlong (); if (L > 99999 | | L < 10000) {


SYSTEM.OUT.PRINTLN ("Input error, please input again!");


L = S.nextlong ();


}for (int i = 4; I >= 0; i--) {a[i] = (int) (L/(long) Math.pow (i));


L = (l% (long) Math.pow (i));


}


Sfyosrtem.out.println (); (int i=0,j=0; i<5; i++, j + +) {


B[J] = A[i];


}for (int i=0,j=4; i<5; i++, j--) {if (a[i)!= B[j]) {


is = false; Break


} else {


is = true;


}


}if (is = = False) {


System.out.println ("is not a palindrom!");


' Else if (is = = = True) {


System.out.println ("is a palindrom!");


}


}}


"Program 26" topic: Please enter the first letter of the week to determine the day of the week, and if the first letter, continue to judge the second letter.


1. Procedure analysis: Use the situation statement is better, if the first letter is the same, then judge with the case statement or if statement to judge the second letter.


Import Java.util.Scanner; public class Ex26 {public static void main (string[] args) {


Save the second letter entered by the user


Char Weeksecond;


Sample the scanner class as an input object to receive user input


Scanner input = new Scanner (system.in);


Start prompting and receive user console input


System.out.print ("Please enter the first letter of the week value in English, I will help you to judge the day of the Week:");


String letter = Input.next ();


Determines whether the user console input string length is a letter


if (letter.length () = = 1) {


Enables scanner to receive a char type input using the character that takes the first index bit


Char Weekfirst = letter.charat (0); Switch (weekfirst) {case ' m ':


When entering lowercase letters, use the switch structure attribute to execute the next case branch with a break statement to implement the work that ignores user console input casing sensitivity


Yes


Case ' M ':


System.out.println ("Monday (Monday)");


Break Case ' t ':


When entering lowercase letters, use the switch structure attribute to execute the next case branch with a break statement to implement the work that ignores user console input casing sensitivity


Yes


Case ' T ':


System.out.print ("as Tuesday (Tuesday) and Thursday (Thursday) all begin with the letter T, so it is necessary to enter a second letter to correctly judge


Broken: ");


letter = Input.next ();


Determines whether the user console input string length is a letter


if (letter.length () = = 1) {


Enables scanner to receive a char type input using the character that takes the first index bit


Weeksecond = Letter.charat (0);


Exploit or (| | ) operator to implement features that ignore user console input case sensitivity


if (Weeksecond = = ' U ' | | weeksecond = = ' U ') {


System.out.println ("Tuesday (Tuesday)");


Break


Exploit or (| | ) operator to implement features that ignore user console input case sensitivity


else if (Weeksecond = = ' h ' | | weeksecond = = ' h ') {


System.out.println ("Thursday (Thursday)");


Break


Console error Prompt


} else{


SYSTEM.OUT.PRINTLN ("Input error, unrecognized week value, second letter, end of program!") ");


Break


}


} else {


Console error Prompt


SYSTEM.OUT.PRINTLN ("Input error, can only enter a letter, program end!") ");


Break


Ca}se ' W ':


When entering lowercase letters, use the switch structure attribute to execute the next case branch with a break statement to implement the work that ignores user console input casing sensitivity


Yes


Case ' W ':


System.out.println ("Wednesday (Wednesday)");


Break Case ' F ':


When entering lowercase letters, use the switch structure attribute to execute the next case branch with a break statement to implement the work that ignores user console input casing sensitivity


Yes


Case ' F ':


System.out.println ("Friday (Friday)");


Break Case ' s ':


When entering lowercase letters, use the switch structure attribute to execute the next case branch with a break statement to implement the work that ignores user console input casing sensitivity


Yes


Case ' S ':


System.out.print ("Since Saturday (Saturday) and Sunday (Sunday) are all started with the letter S, so you need to enter a second letter to correctly judge:");


letter = Input.next ();


Determines whether the user console input string length is a letter


if (letter.length () = = 1) {


Enables scanner to receive a char type input using the character that takes the first index bit


Weeksecond = Letter.charat (0);


Exploit or (| | ) operator to implement features that ignore user console input case sensitivity


if (Weeksecond = = ' a ' | | weeksecond = = ' a ') {


System.out.println ("Saturday (Saturday)");


Break


Exploit or (| | ) operator to implement features that ignore user console input case sensitivity


else if (Weeksecond = = ' U ' | | weeksecond = = ' U ') {


System.out.println ("Sunday (Sunday)");


Break


Console error Prompt


} else{


SYSTEM.OUT.PRINTLN ("Input error, unrecognized week value, second letter, end of program!") ");


Break


}


} else{


Console error Prompt


SYSTEM.OUT.PRINTLN ("Input error, can only enter a letter, program end!") ");


Break


De}fault:


Console error Prompt


SYSTEM.OUT.PRINTLN ("Input error, unrecognized week value first letter, end of program!") ");


Break


}


} else{


Console error Prompt


SYSTEM.OUT.PRINTLN ("Input error, can only enter a letter, program end!") ");


}


}


}


"Program 27" topic: Find the prime number within 100


public class Ex27 {public static void main (String args[])


{int sum,i; for (sum=2;sum<=100;sum++)


{for (i=2;i<=sum/2;i++)


{if (sum%i==0) break;


}if (I>SUM/2)


System.out.println (sum+ "is prime");


}


}


}


"Program 28" topic: Sorting 10 numbers


1. Program Analysis: You can use the selection method, that is, from the last 9 comparison process, select a minimum and the first element of the exchange, the next analogy, that is, with the second element and the last 8, to keep abreast


Row Exchange.


Import Java.util.Arrays; Import Java.util.Random; Import Java.util.Scanner; public class Ex28 {public static void main (string[] args) {int arr[] = new INT[11];


Random r=new Random (); for (int i=0;i<10;i++) {


Arr[i]=r.nextint (100) +1;//gets 10 integers within 100


}


Arrays.sort for (arr); (int i=0;i<arr.length;i++) {


System.out.print (arr[i]+ "T");


}


System.out.print ("\nplease Input a int number:");


Scanner sc=new Scanner (system.in);


Arr[10]=sc.nextint ()//Enter an int value


Afrorrays.sort (arr); (int i=0;i<arr.length;i++) {


System.out.print (arr[i]+ "T");


}


}


}


"Program 29" topic: Finding the sum of diagonal elements of a 3*3 matrix


1. Program Analysis: Use dual for loop control input two-dimensional array, and then add a[i][i] after the output.


public class Ex29 {public static void main (string[] args) {double sum=0; int array[][]={{1,2,3},{4,5, 6},{7,7,8}}; T i=0;i<3;i++) for (int j=0;j<3;j++) {if (i==j)


Sum=sum + array[i][j];


}


SYSTEM.OUT.PRINTLN (sum);


}


}


"Program 30" topic: There is a sorted array. Now enter a number that requires inserting it into the array as it was.


1. Procedure analysis: First determine whether this number is greater than the last number, and then consider inserting the middle number of cases, after inserting the number of this element, and then move one position.


Import Java.util.Random; public class ArraySort {public static void main (string[] args)


{int temp=0; int myarr[] = new INT[12];


Random r=new Random (); for (int i=1;i<=10;i++)


Fomryarr[i]=r.nextint (1000); (int k=1;k<=10;k++)


FSoyrstem.out.print (myarr[k]+ ","); (int i=1;i<=9;i++) for (int k=i+1;k<=10;k++) if (myarr[i]>myarr[k))


{


Temp=myarr[i];


MYARR[I]=MYARR[K];


Myarr[k]=temp;


}


FSoyrstem.out.println (""); (int k=1;k<=10;k++)


System.out.print (myarr[k]+ ",");


Fmoyarrr[11]=r.nextint (1000); (int k=1;k<=10;k++) if (myarr[k]>myarr[11])


{


FTEOMRP=MYARR[11]; (int j=11;j>=k+1;j--)


MYARR[J]=MYARR[J-1];


Myarr[k]=temp;


}


ForSystem.out.println (""); (int k=1;k<=11;k++)


System.out.print (myarr[k]+ ",");


}


}


"Program 31" topic: output an array in reverse order.


Program Analysis: Use the first to exchange with the last one.


In fact, it's simpler to use a loop control variable:


for (int k=11;k>=1;k--)


System.out.print (myarr[k]+ ",");


"Program 32" topic: Take an integer A from the right end of the 4~7 bit.


Program Analysis: You can consider this:


(1) first move a 4-bit right.


(2) Set a low 4-bit total of 1, the rest are all 0 of the number. Available ~ (~0 < <4)


(3) The above two carry on the & operation.


public class Ex32 {public static void main (string[] args)


{int a=0; long b=18745678;


a= (int) Math.floor (b% Math.pow (10,7)/math.pow (10, 3));


System.out.println (a);


}}


"Program 33"


Title: Print out the Yang Hui's triangle (required to print out 10 lines as shown below)


1. Procedure Analysis:


1


1 1


1 2 1


1 3 3 1


1 4 6 4 1


1 5 10 10 5 1


public class Ex33 {public static void main (String args[]) {int i,j; int a[][];


A=new Int[8][8]; for (i=0;i<8;i++) {


A[i][i]=1;


A[i][0]=1;


for} (i=2;i<8;i++) {for (j=1;j<=i-1;j++) {


A[I][J]=A[I-1][J-1]+A[I-1][J];


}


}for (i=0;i<8;i++) {for (j=0;j<i;j++) {


System.out.printf ("" "+a[i][j]);


}


System.out.println ();


}


}


}


"Program 34" topic: Enter 3 number a,b,c, output in order of size.


1. Procedure analysis: Using pointer method.


public class Ex34 {public static void main (string[] args)


{int []arrays = {800,56,500}; for (int i=arrays.length;--i>=0;)


{for (int j=0;j<i;j++)


{if (arrays[j]>arrays[j+1])


{int temp=arrays[j];


ARRAYS[J]=ARRAYS[J+1];


Arrays[j+1]=temp;


(int n=0;n<arrays.length;n++)}}}


System.out.println (Arrays[n]);


}


}


"Program 35" topic: input Array, the largest exchange with the first element, the smallest with the last element exchange, output array.


Import java.util.*; public class Ex35 {public static void main (string[] args) {int i, MIN, Max, N, Temp1, temp2; int a[];


System.out.println ("Length of input array:");


Scanner keyboard = new Scanner (system.in);


n = keyboard.nextint ();


A = new Int[n]; for (i = 0; i < n; i++) {


System.out.print ("Input First" + (i + 1) + "data");


A[i] = Keyboard.nextint ();


}


The above is the input of the entire array


max = 0;


min = 0;


Set two flags, starting with the first number


for (i = 1; i < n; i++) {if (A[i] > A[max])


max = i; Iterate over the array, if greater than A[max], assign his array subscript to Max


if (A[i] < a[min])


min = i; Ibid., if less than a[min], assign his array subscript to min


}


The above for loop finds the maximum and minimum values, Max is the subscript for the maximum, and Min is the subscript for the minimum value.


Temp1 = a[0];


Temp2 = A[min]; These two temp are just for use in exchange


A[0] = A[max];


A[max] = Temp1; First swap a[0] and Max A[max]


if (min!= 0) {//If the minimum value is not a[0], perform the following


A[min] = a[n-1];


A[n-1] = Temp2; Exchange A[min] and a[n-1]


else {//If the minimum value is a[0], perform the following


A[max] = a[n-1];


A[n-1] = Temp1;


}


for (i = 0; i < n; i++) {//output array


System.out.print (A[i] + "");


}}}


"Program 36" topic: There are n integers, so that the order of the previous number of each move backward m position, the last m number into the first number of M


"Program 37"


Title: There are n people in a circle, the order automatic arranging. From the first person to start off (from 1 to 3 off), where 3 of the people who reported out of the circle, asked the last left is the original number of that.


Import Java.util.Scanner; public class Ex37 {public static void main (string[] args) {


Scanner s = new Scanner (system.in); int n = s.nextint (); boolean[] arr = new Boolean[n]; for (int i=0; i<arr.length; i++) {


Arr[i] = true;//is still in the loop when the subscript is true


}int leftcount = n; int countnum = 0; int index = 0; while (Leftcount > 1) {


if (arr[index] = = True) {//when in a circle


Countnum + +; Numbered accumulator


if (Countnum = 3) {//Report 3 o'clock


Countnum =0;//continues to count from zero.


Arr[index] = false;//This person out of the circle


Leftcount--;//remaining number minus one


}


}


Index ++;//every newspaper, subscript plus a


if (index = = N) {//is the number of loops, the current is greater than N, the description has been counted a circle,


index = 0;//Sets the subscript to zero to start again.


}


}for (int i=0; i<n; i++) {if (arr[i] = = True) {


System.out.println (i);


}


}


}


}


"Program 38"


Title: Write a function, ask for the length of a string, enter a string in the main function, and output its length.


Import Java.util.Scanner; public class Ex38 {public static void main (String [] args)


{


Scanner s = new Scanner (system.in);


System.out.println ("Please enter a string");


String mys= S.next ();


System.out.println (Str_len (MYS));


public static int Str_len (String x)


{return x.length ();


}


}


Topic: Write a function, enter n is even, call function to 1/2+1/4+...+1/n, when input n is odd, call function 1/1+1/3+...+1/n


"Program 39"


Topic: string sorting.


Import java.util.*; public class test{public static void Main (string[] args)


{


Arraylist<string> list=new arraylist<string> ();


List.add ("010101");


List.add ("010003");


List.add ("010201");


Collections.sort for (list); (int i=0;i<list.size (); i++) {


System.out.println (List.get (i));


}}}


"Program 40"


Title: There is a pile of peaches on the beach, five monkeys to score. The first monkey put the pile of peach credentials into five, one more, the monkey threw a lot of one into the sea, took a copy. Second One


The monkey divided the remaining peaches into five, and one more, it also put more than one into the sea, took a copy, third, four, fifth monkeys are doing so, asked the beach on the original


How many peaches are there at least?


public class Dg {


static int ts=0;//Peach Total


int fs=1;//number of record points


static int hs=5;//Monkey number ...


The range of the number of peaches in int tsscope=5000;//. Too big to overflow.


public int FT (int t) {if (T==tsscope) {


Cancel recursion when the peach is counted to the maximum range


System.out.println ("End");


return 0;


}else{if ((t-1)%hs==0 && fs <=hs) {if (FS==HS)


{


SYSTEM.OUT.PRINTLN ("Peach number =" +ts + "when satisfying the peach condition");


}


Fs+=1;


Return FT ((t-1)/5*4); go back to the monkey and take a copy of the remaining total


}else


{


I didn't meet the conditions.


Reset the number of fs=1;//points to 1


Return FT (ts+=1)//Peach number plus +1


}}}


public static void Main (string[] args) {new Dg (). FT (0);


}


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.