1. What is an algorithm
Your own understanding is the way to solve the problem.
The essence of work is to solve the problem--Peter Drucker
2. Cycle
Loop four elements: Initialize variables, loop conditions, change iterations of loop variables, loop code blocks
While: first judge the cycle conditions, after the execution of the loop body;
Do While: first the loop body is executed, then the cyclic condition is judged;
The difference between the two: under normal operating conditions, while will be more than do while more judgment, the output is consistent;
In the case of abnormal operation, the loop body in while is not executed, and the Do while runs the loop body;
Public classalgorithm{ Public Static voidMain (string[] args) {intA = 1; while(a<5) {System.out.println (a); A++; } intb = 1; Do{System.out.println (b); b++; } while(b<5); } }View Code
Find out all narcissus numbers:
Public classalgorithm{//number of daffodils: a three-digit number 1000 cubic and equal to the number itself; Select all narcissus numbers Public Static voidMain (string[] args) {intA = 100; while(a<1000){ intb = a/100; intc = a/10%10; intD = a%10; if(A = = B*b*b + c*c*c +d*d*d) {System.out.println (a); } A= A+1; } } }View Code
Public classalgorithm{//number of daffodils: a three-digit number 1000 cubic and equal to the number itself; Select all narcissus numbers Public Static voidMain (string[] args) { for(intA = 100; a<1000; a++){ intb = a/100; intc = a/10%10; intD = a%10; if(A = = b*b*b+c*c*c+d*d*d) {System.out.println (a); } } } }View Code
Iterate through the array to find the maximum/minimum value
Public classalgorithm{//traverse Array a[] to find the maximum value Public Static voidMain (string[] args) {intA[] = {-}; intx = a[0]; for(inti=0; i<a.length; i++){ if(a[i]>x) {x=A[i]; }} System.out.println (x); } }View Code
Bubble sort
Public classalgorithm{//iterate over array a[], sorted by large to small Public Static voidMain (string[] args) {intA[] = {1,3,2,8,7}; for(inti=0;i<a.length-1;i++){ for(intj=0;j<a.length-1-i;j++){ if(a[j]<a[j+1]){ intx=A[j]; A[J]=a[j+1]; A[j+1] =x; } } } for(inti=0;i<a.length;i++) {System.out.println (a[i]); } } }View Code
Data structure and algorithm (i.)