INT-102_Lecture_01
作者:互联网
Part I
-
Some Well-known Computational Problems
- Sorting
- Searching
- Graph and Combinatorial Problems
- Shortest paths in a graph---【最短路】
- Minimum spanning tree
- Traveling salesman problem
- Knapsack problem
- ...
- Problem in Number Theory
- Primality testing
- ...
-
What is an algorithm?
- A sequence of precise and concise 【精确且简洁】instructions that guide you (or a computer) to solve a class of specific problem
-
Learning Aims
- To give an overview of the study of algorithms in terms of their efficiency
- To introduce the standard algorithmic design patterns 【设计模式】employed in the development of efficient algorithmic solutions
- To describe the analysis of algorithms in terms of the use of formal models of Time and Space
- To give a brief introduction to the subject of computational complexity theory 【计算复杂度】and its use in classifying computational problems
Part II: How to represent algorithms
-
Pseudo Code
-
Trace Table & Statement
-
p = 1 for i = 1 to n do p = p * x output p
- Statement
- Assignment statement【赋值语句】
-
Variable = Expression
- e.g., assign the expression 3 * 4 to the variable result :
-
result = 3 * 4
-
-
- Compound statements 【复合语句】
-
begin statement1 statement2 end
-
- Conditional statement 【条件语句】
-
if condition then statement
-
if condition then statement1 else statement2
- Example:
-
if a < 0 then a = -a abs = a output abs
-
if a > 0 then abs = a else abs = -a output abs
-
-
- Iterative statement 【迭代语句】
- For Loop 【For 循环】
-
for var = start_value to end_value do statement
- Example: Computing sum of the first n numbers
-
input n sum = 0 for i = 1 to n do begin sum = sum + i end output sum
- The loop is executed for n times
-
-
- While Loop 【While循环】
-
while condition do statement
- Condition__CONTINUE the loop
- Example_01: Computing sum of the first n numbers
-
input n sum = 0 i = 1 while i <= n do begin sum = sum + i i = i + 1 end output sum
-
- Example_02: Computing sum of all (keyboard) input numbers
-
sum = 0 while (user wants to continue) do begin ask for a number sum = sum + number end output sum
- This loop is executed for an undetermined number of times
-
-
- Repeat-until Statement 【Repeat循环】
-
repeat statement until condition
- Condition__STOP the loop
- Example: Computing sum of all (keyboard) input numbers
-
sum = 0 repeat ask for a number sum = sum + number until (user wants to stop) output sum
-
-
- For Loop 【For 循环】
- Assignment statement【赋值语句】
-
-
Computing the n-th power
- Input
- A number x & A non-negative integer n
- Output
- The n-th power of x
- Algorithm
- Set a temporary variable p to 1
- Repeat the multiplication p = p * x for n times
- Output the result p
- Input
-
Algorithm vs Program
- Algorithms
- Free from grammatical rules
- Content is more important than form
- Acceptable as long as it tells people how to perform a task
- Programs
- Must follow some syntax rules
- Form is important
- Even if the idea is correct, it is still not acceptable if there is a syntax error
- More Generally: Program = Data Structure + Algorithm
- Algorithms
Part III: Pseudo Code_Exercise
- Ex_01:List all factors of a given positive integer x
- For Loop
-
for i = 1 to x do begin if (x%i == 0) then output i end
-
- While Loop
-
i = 1 while i <= x do begin if x%i == 0 then output i i = i+1 end
-
- Repeat Loop
-
i = 1 repeat if x%i == 0 then output i i = i+1 until i > x
-
- For Loop
- Ex_02:Find the product of all integers in the interval [x, y], assuming that x and y are both integers
- For Loop
-
product = 1 for i = x to y do begin product = product * i end output product
-
- While Loop
-
product = 1 i = x while i <= y do begin product = product * i i = i+1 end output product
-
- Repeat Loop
-
product = 1 if x <= y then begin i = x repeat product = product * i i = i+1 until i > y output product end
-
- For Loop
Part IV: Summary
- While
- 判断==>执行
- 满足条件,执行,继续循环
- Repeat
- 执行==>判断
- 满足条件,不执行,结束循环
- Loop
- 执行==>判断
- 满足条件,不执行,结束循环
- 当条件为False时,Repeat循环也能执行一次,类似于 java中的 Do While 循环; 而 While、Loop 循环无法执行
标签:do,product,01,INT,sum,statement,102,output,Loop 来源: https://blog.csdn.net/Astronaut_WHY/article/details/123221502