Algorithm application hundred money to buy white Chicken case Description: The main content is: Rooster 5 Yuan A, hen 3 yuan A, chicken 3 yuan A, ask 100 yuan how can buy 100 chicken? Thought: Want to implement this algorithm, as long as understand the relationship of various conditions can, and know the rooster to buy 20, hen up to buy 33, chicken up to buy 100, so buy all kinds of chicken money is always 100, Yuan, chicken's only number is 100; Implementation code:
Packagealgorithm Application;/** * * @authorAdministrator **/ Public classBQMJ { Public Static voidMain (string[] args) {intCock,hen,chicken=0; for(cock=0;cock<=19;cock++){ for(hen=0;hen<=33;hen++) {Chicken=100-cock-hen; intp; P=chicken%3; if((5*COCK+3*HEN+CHICKEN/3) ==100) && (p==0) {System.out.print ("Can buy only the number of cocks:" +cock); System.out.print ("Can buy only the number of hens:" +hen); System.out.print ("The only number of chickens you can buy:" +chicken); System.out.println ("\ n"); } } } }}
Operation Result:
The application of the method of the Han Xin Soldiers of case description: Han Xin soldiers less than hundred people, 3 people line more than one person, 7 people line up a few two people, 5 people line just, this example is the calculation of Han Xin exactly how many troops? Idea: For the Han Xin Soldiers of algorithm, only 7 people less than 2 people convert 7 people more than 5 people, so that the way to solve the problem is obvious, and then limit the number of people not more than 100. Implementation code:
Packagealgorithm Application; Public classHxin { Public Static voidMain (string[] args) {intA=0,b=0,c=0,preson;//define the total number of people and the remainder of the various station methods for(preson=0;preson<100;preson++) {a=preson%3;//number of three people left in each rowb=preson%7;//the remaining number of seven people per rowc=preson%5;//the remaining number of five people per row if(a==1&&b==5&&c==0) {//the number of people who meet the criteriaSystem.out.println ("Han Xin with the number of soldiers is:" +Preson); } } }}
The result is: 40 people
Fibonacci sequence of the algorithm application description: The Fibonacci sequence definition: its first and second items are 1, and later are the first two and the difficult: how to design a loop implementation code:
Packagealgorithm Application;ImportJava.util.Scanner; Public classFbo {Private Static voidFintx) { intF1=1,f2=1,i=3; if(x==1) System.out.print (F1); if(x==2) System.out.print (f1+ "" +F2); if(x>=3) {//to find a series of positions greater than threeSystem.out.print (f1+ "" +F2); while(x>=i) {//Find a seriesF1=F2+F1;//To seek the sum of two itemsSystem.out.print ("" +F1); I++; F2=f2+F1; System.out.print (" "+F2); } } } Public Static voidMain (string[] args) {Scanner s=NewScanner (system.in); System.out.println ("Please enter the number of Fibonacci numbers you would like to see:"); intnum=S.nextint (); System.out.println ("You want to see the Fibonacci sequence:"); F (Num/2+1); }}
Operation Result:
The Chinese Nota case of the algorithm application description: Hanoi tower problem is a classical mathematical problem, the content is: Hanoi (also known as Hanoi) problem is originated in India an ancient legend of educational toys. When big Brahma created the world, he made three diamond pillars, and stacked 64 gold discs on a pillar from bottom to top in order of size. The great Brahma commanded the Brahman to rearrange the discs from below to the other pillars in order of size. It is also stipulated that the disc cannot be enlarged on the small disc, and only one disc can be moved between the three pillars at a time. Difficulty: moving n plates from one seat to another, this is what each mover wants to do, except for the first mover, the rest is to command the other movers, that is, the first mover begins, the task layer is lowered, and finally a plate is moved from one seat to another, which is the first mover to do his own work,
Implementation code:
Packagealgorithm Application;ImportJava.util.Scanner; Public classHanoi {Private Static voidMoveCharXChary) {System.out.printf ("%c-->%c", x, y); System.out.print ("\ n"); } Private Static voidHanoit (intNCharOneCharBoth,CharThree) {//move n Plates from the first seat to the third seat with the second seat . if(n==1) {//If there's only one plateMove (One,three); } Else{hanoit (n-1,one,three,two);//To move a plate on one of three to two.Move (One,three); Hanoit (n-1,two,one,three);//move the plates on the second one to three . } } Public Static voidMain (string[] args) {intm; System.out.println ("Please enter the number of plates you want to move:"); Scanner s=NewScanner (system.in); M=S.nextint (); System.out.println (The steps to "move" the +m+ "plate are as follows"); Hanoit (M,' A ', ' B ', ' C '); }}
Operation Result:
Java algorithm Application