Data structure and algorithm (i.)

Source: Internet
Author: User

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.)

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.