d5133e9dab258e86617d31396ba14ebf.ppt
- Количество слайдов: 10
CS 100 A, Lect. 9, 29 Sept. 1997 More on Iteration We’ll develop some loops of the form: < initialization to make invariant true> // general picture, or invariant P: . . . while ( B )
// B is false, and from this and invariant P // we can see that the result has been // achieved CS 100 A, 29 Sept. 1997. Lecture 9Problem: Given n>0, set s to the largest power of 2 that is at most n. n 1 2 3 4 5 6 7 8 9 … 50 63 64 65 s 1 2 2 4 4 8 8 32 32 64 64 20 = 1 21 = 2 22 = 2*2 = 4 23 = 2*2*2 = 8 25 = 2*2*2 = 32 26 = 2*2*2*2 = 64 CS 100 A, 29 Sept. 1997. Lecture 9
Strategy: Start s at 1 and continue to multiply it by 2 until the next multiplication by 2 would make it too big. General picture (invariant) s is a power of 2 and s <= n Initialization: s= 1; Stop when: 2*s > n Continue as long as: 2*s <= n Make progress toward termination and keep general picture true: s= 2*s; // Known: n>0. Set s to the largest power of // 2 that is at most n s= 1; //invariant: 2 is a power of 2 and s <= n while (2*s <= n) s= 2*s; CS 100 A, 29 Sept. 1997. Lecture 9
Example of a loop: logarithmic spiral containing n lines en gre 3 red (hc, vc) ue bl 1: length d 2: length 2*d 3: length 3*d … k: length k*d. . . 2 turn degrees e blu 1 4 Each line turns turn degrees to the left of its predecessor CS 100 A, 29 Sept. 1997. Lecture 9
// The spiral consists of n line segments. // Line segment 1 has starting point (hc, vc). // Line segment k, for 1<=k<=n, has length k*d. // Each line segment makes an angle of turn degrees // with the previous line segment. // Line colors alternate between blue, green, red final static int hc= 300; final static int vc= 250; final static int n= 200; final static int turn= 121; final static double d=. 2; // Center of spiral is (hc, vc) // Number of sides to draw // The turn factor // Length of leg k is k*d We demonstrate execution of this program on the Macintosh, using also n = 10, 000 and different values of turn (85, 89, 90, 85, 135, 180, 150), The java source will be on the CS 100 A web site CS 100 A, 29 Sept. 1997. Lecture 9
public void paint(Graphics g) { int h= hc; int v= vc; int k= 1; //Invariant: lines 1. . k-1 have been drawn, and line k // is to be drawn with start point (h, v) while (k<=n) { //Draw line k if (k%3==0) g. set. Color(Color. red); if (k%3==1) g. set. Color(Color. blue); if (k%3==2) g. set. Color(Color. green); int theta= k*turn %360; double L= k*d; // Length of line k // Compute end (h_next, v_next) of line k int h_next= (int) Math. round ( CS 100 A, 29 Sept. 1997. Lecture 9 h+L*Math. cos(theta*Math. PI/180));
Write a method with the following heading: // Return the position of c in s (or s. length() // if c is not in s) public static int find(char c, String s) Examples c s ‘a’ “All’s well that ends” ‘A’ “All’s well that ends” 0 ‘i’ “All’s well that ends” 20 ‘w’ “” ‘’ “” CS 100 A, 29 Sept. 1997. Lecture 9 result 13 0 1 0
Strategy: look at the characters of s one by one, starting from the beginning. Return as soon as c is found. Use a variable j to tell how much of s has been “scanned” (looked at). General picture: c is not in s[0. . j-1], i. e. c is not one of the first j characters of s, and 0 <= j <= s. length(). Initialization: j= 0; Not Java notation When can the loop stop? When j= s. length() or c = s[j]. So the loop condition is j != s. length() && s. char. At(j) != c CS 100 A, 29 Sept. 1997. Lecture 9
What should be done in the body to get closer to termination? Add one to j. What should be done in the body to keep the general picture true? Nothing. // Yield the position of c in s (yield s. length() // if c is not in s) public static int find(char c, String s) {int j= 0; // Invariant: c is not in s[0. . j-1] and // 0 <= j <= s. length(). while (j != s. length( ) && c != s. char. At(j)) j= j+1; return j; } CS 100 A, 29 Sept. 1997. Lecture 9
Another version: // Yield the position of c in s (yield s. length() // if c is not in s) public static int find(char c, String s) {int j= 0; // Invariant: c is not in s[0. . j-1] and // 0 <= j <= s. length(). while (j != s. length( )) {if c = s. char. At(j) return j; j= j+1; } return j; } CS 100 A, 29 Sept. 1997. Lecture 9