2

algorithm


  • Comparison of the two algorithms
  • The uniqueness of the algorithm

1. Write a program that asks 1 to 100. Most people will write the following code immediately

Writing 1

  // 容器
  int sum = 0;
  
  for (int i = 1; i <= 100 ; i++) {
        // 1+2+3+4...加到100 
        sum = i + sum;
  }
       // 输出结果 5050
        System.out.println(sum);


The question is, is it really good to write this way, or is it the most efficient?


This is when Gauss came up with an algorithm for finding arithmetic sequence when he was a child

Writing 2


int sum = 0, n =100;
        // (1 + 100) * 50 = 5050
        sum = (1+ n) * n/2;
        // 输出结果 5050
        System.out.println(sum);

Writing method 1 The algorithm needs to be added one by one for each calculation.
How to write 2 he knew the result in an instant.


algorithm features:
Input and output finiteness + certainty + feasibility = algorithm.


output input :

input : It is generally necessary to have 0 or more inputs with parameters.
output : The algorithm must have one or more outputs, and no output is required. Why do you write this algorithm?
The output format can be print or return one or more values .


has :
Means that the algorithm will automatically end after performing useful steps without the wireless loop, and it be completed in an acceptable time for 160d1d7d733e46.


certainty : Every step algorithm has determine the significance will not appear ambiguity
Under certain conditions, the algorithm has only one input with the same execution path and only a unique result.


Feasibility The algorithm is feasible at each step, and each step must be executed a limited number of times.


Abbreviated as : fast, accurate and stable must be accurate and stable-logically.


The learning content is: <Dahua Data Structure>


嘻嘻硕
27 声望12 粉丝

想当一只天然呆的鸭qwq


引用和评论

0 条评论