Download presentation

Presentation is loading. Please wait.

Published byLance Crome Modified over 2 years ago

1
Advanced Programming 15 Feb 2003

2
The “OI” Programming Process Reading the problem statement Thinking Coding + Compiling Testing + Debugging Finalizing the program

3
Reading… Read the problem statements carefully Note the constraints Meaningful Range of variables Execution time limit Do not “guess” the meaning of the problem statements.

4
Thinking What is the ‘type’ of the problem? DP? Graph? Maths? Others? Have you solved a similar problem before? How do you solve the problem without computer? Can the problem be further divided into some easier sub-problems?

5
Thinking (cont.) Drawing diagrams may be useful (especially for DP and Graph problems) Is the problem too simple? If yes, look for tricks ;-) Is the algorithm correct? Any special cases? Have you coded it before? What were the difficulties you faced?

6
Thinking (cont.) Is the algorithm feasible? How to code it in PASCAL? Is it easy to code? What is the approximate execution time? How to store the data? Integer? LongInt? String? Array of char? Array? Linked-List? Are there enough memory to store the data?

7
Thinking (cont.) Are there any (simple) ways to improve Program execution time? Ease of coding?

8
Coding Pre-processing (e.g. find primes) Input Process Part 1, 2, …, N Output Save and compile your code frequently

9
Coding (cont.) Avoid long and complex expressions a:=2*(x2-x1); b:=2*(y2-y1); c:=x1*x1+y1*y1-x2*x2-y2*y2; d:=2*(x3-x1); e:=2*(y3-y1); f:=x1*x1+y1*y1-x3*x3-y3*y3; x:=(b*f-c*e)/(a*e-b*d); y:=(c*d-a*f)/(a*e-b*d); r:=sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1)); x:=(2*(y2-y1)*(x1*x1+y1*y1-x3*x3-y3*y3)- 2*(x1*x1+y1*y1-x2*x2-y2*y2)*(y3-y1)) /(4*(x2-x1)*(y3-y1)-4*(y2-y1)*(x3-x1)); y:=((x1*x1+y1*y1-x2*x2-y2*y2)*2*(x3-x1)- 2*(x2-x1)*(x1*x1+y1*y1-x3*x3-y3*y3)) /(4*(x2-x1)*(y3-y1)-4*(y2-y1)*(x3-x1)); r:=sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1));

10
Coding (cont.) Copy-and-paste Make sure the “source” is correct Update all variables Double checking required

11
Testing When to test your program? After completing the whole program? After writing each statement? After coding each part? After making changes to a part?

12
Testing (cont.) Print all important variables to screen Print messages Use debugger, inspect program behavior by running step-by-step

13
Testing (cont.) Sample Output “A problem has Sample Output for two reasons: 1. To make you understand what the correct output format is 2. To make you believe that your incorrect solution has solved the problem correctly ”

14
Testing (cont.) Hand-made Input Boundary Case “Large” Input Execution Time Overflow

15
Debugging Some short-cut keys F4 – Goto Cursor F7 – Trace into F8 – Step Over Ctrl-F7 – Add Watch Ctrl-F2 – Program Reset Ctrl-F8 – Add BreakPoint Alt-F5 – Screen Output

16
Finalizing Check I/O filename Check Output Format Trailing spaces? Correct source/executable name? Is the executable updated?

17
Demostration HKOI2003 Junior Q3 – Goldbach’s Conjecture Read the problem statements, and think about the algorithms now ;-)

18
Reading… Given an integer N, find two primes p 1,p 2 such that p 1 +p 2 =N p 1 ≤ p 2 Minimize (p 2 -p 1 ) Minimum N = 4 Maximum N = 10,000,000 (wow!) Can N be odd?

19
Thinking… Find two primes… if M mod p i = 0, where p i is a prime, then M is not prime. So….the first step is to find some small prime numbers (up to ) Then, write a function that uses these prime numbers to check for larger primes Use a for-loop to find two prime numbers such that their sum is N Feasible? Easy to write?

Similar presentations

OK

Chapter 5 Repetition and Loop Statements J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.

Chapter 5 Repetition and Loop Statements J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google