计算机算法简介 - Lu Jiaheng's homepage

Download Report

Transcript 计算机算法简介 - Lu Jiaheng's homepage

计算机科学概述
Introduction to Computer Science
陆嘉恒
中国人民大学 信息学院
www.jiahenglu.net
Computer Algorithm
(计算机算法简介)
An Algorithm is
A well-ordered collection of
Unambiguous and
Effectively computable operations that, when
executed
Produces a result and
Halts in a finite amount of time
Textbook definition
Representing Algorithms
How do we represent algorithms?
• English
• Programming Language
• We will start with pseudocode and later
will look at various programming
languages.
Pseudocode
• Set of English Constructs designed to
resemble statements in programming
languages
• Steps presented in structured manner
• Simple, Highly readable
Constructs We Will Use
• Sequential operations to carry out
– computation,
– input, and
– output.
• Conditional operations.
• Iterative operations.
Sequential Operations
• computation
•
•
•
•
Set the value of X to 3.
Assign X a value of A + B.
X=2 - C.
Set the value of Name to the first person's name.
• input
• Get a value for X, Y, and Z.
• Input values for A1, A2, ..., Am.
• Read X, Y, and Carry.
• output
• Output the value of X.
• Print values for X, Y, and Carry.
• Print the message, "Error".
Example Algorithm (Sequential)
Figure 2.3
Algorithm for Computing Average Miles per Gallon
Practice
Write an algorithm in pseudocode to:
Get the radius of a circle as input and output
the circumference and area of the circle
Conditional Operation
If “a true/false condition” is true then
first set of algorithmic operations
Else
second set of algorithm operations
Notice the indentation
Example Algorithm (conditionals)
Figure 2.5
Second Version of the Average Miles per Gallon Algorithm
Practice
Modify your algorithm to print “large circle” if
the circumference is greater than 100 and
“small circle” if it is equal to or less than
100.
Conditional Operation Picture
true
true
branch
operation
true-false
statement
false
operation
false
branch
operation
operation
Note: either branch
can be missing
Conditional Operation Picture
true
true
branch
operation
true-false
statement
false
operation
false
branch
operation
operation
Note: either branch
can be missing
Conditional Operation Picture
true
true
branch
operation
true-false
statement
false
operation
false
branch
operation
operation
Note: either branch
can be missing
Iterative Operations
Set count to 5
While count < 4
Add 1 to count
Print count
stop
Pretest loop
Set count to 5
Do
Add 1 to count
Print count
While count < 4
stop
Posttest loop
Notice when the continuation condition is checked
Infinite Loops
Set count to 0
While count >= 0
Add 1 to count
Print count
stop
Iteration Operation Picture
true-false
statement
false
operation
true
operation
operation
operation
false
while loop
true-false
statement
Do/while loop
true
Example Algorithm (iteration)
Figure 2.7
Third Version of the Average Miles per Gallon Algorithm
Practice
Write an algorithm that gets as input a single
data value, x, and outputs the three values
x2, sin x, 1/x. This process should repeat
until the input value for x is equal to 99.
Summary of Operation Types
for Algorithms
• Sequential operations to carry out
– computation,
– input, and
– output.
• Conditional operations.
• Iterative operations.
Powerful Algorithms!!
There is a theorem in theoretical computer science that proves that
these operations are sufficient to represent ANY algorithm!
Algorithms are used to do everything you see on a computer!
•
•
•
•
•
•
•
Do word processing.
Fly NASA probes to the planets.
Run the international telephone switching system.
Create CAT scan images.
Process your pay checks.
Run computer games.
Etc.
EXAMPLE OF AN ALGORITHM
PROBLEM: Start with a collection of names N1, N2, ...,
N10000, and corresponding telephone numbers T1, T2, ...,
T10000. Given a name, Name, find and print the telephone
number for that name if a match on an Ni occurs;
otherwise, print "Not Found".
Given a problem, there are often many ways to provide an
algorithm for solving the problem.
Note: You must understand the methodology for solving
the problem in order to write an algorithm for the
solution!!!
First Try
1.
2.
Get values for N1, N2, ..., N10000, T1, T2, ,,,, T10000, and Name.
If Name is N1, then
print T1
Stop
3.
If Name is N2, then
print T2.
Stop.
{a lot of tedious writing here that is being skipped}
10001. If Name is N10000, then
print T10000.
Stop.
10002. Print "Not found"
10003. Stop.
Second Try
1. Get values for N1, N2, ..., N10000, T1, T2, ,,,, T10000, and Name.
2.
Set the value of i to 1 and the value of Found to NO.
3.
While Found is NO
4.
If Name is equal to Ni, then
5.
Print the telephone number Ti
6.
Set the value of Found to Yes
Else
7.
8. Stop.
Add 1 to the value of i
Third Try
1. Get values for N1, N2, ..., N10000, T1, T2, ,,,, T10000, and Name.
2. Set the value of i to 1 and the value of Found to NO.
3. While Found is NO and i <= 10000
4.
If Name is equal to Ni, then
5.
Print the telephone number Ti
6.
Set the value of Found to Yes
Else
7.
Add 1 to the value of i
8. If (Found is No) then
9.
Print "Not found"
10. Stop.
Game Time
Who can beat me in the game 21?
Objective:
Get your opponent to say the number 21.
Rules:
One player goes first starting counting from 1.
On each turn a player may say one or two consecutive numbers.
You must begin where your opponent left off.
The first person to say the number 21 loses.
There is a way to always beat me!