Experiment 1 basics of algorithm Problem Solving-Euclidean and recursive algorithms, Euclidean Recursion
1. Experiment Name: Basics of solving algorithm problems
Ii. Tutorial Purpose
Exercise the following algorithms
1. Euclidean Recursive Algorithm
2. Euclidean Iteration Algorithm
3. Continuous integer Detection Algorithm
4. Recursive Algorithms
Iii. experiment content
Part 1 Euclidean Algorithm
Problem:
1. calculation: the maximum number of public appointments between 34931 and 75236:
1 # include <iostream. h> 2 3 // 1. euclidean recursive algorithm 4 void Swap (int & a, int & B) 5 {6 int c = a; a = B; B = c; 7} 8 9 int RGcd (int m, int n) 10 {11 if (m = 0) 12 return n; 13 return RGcd (n % m, m ); 14} 15 16 int Gcd1 (int m, int n) // control program 1-117 {18 19 if (m> n) 20 {21 Swap (m, n ); 22} 23 return RGcd (m, n); 24} 25 26 27 // 2. euclidean iteration algorithm 28 int Gcd2 (int m, int n) // control program 1-229 {30 if (m = 0) 31 {32 return n; 33} 34 if (n = 0) 35 {36 return m; 37} 38 if (M> n) 39 {40 Swap (m, n); 41} 42 while (m> 0) {43 int c = n % m; n = m; m = c; 44} 45 return n; 46} 47 48 // 3. continuous integer detection algorithm 49 50 int Gcd3 (int m, int n) // control program 1-351 {52 // student Input Program Part 53 if (m = 0) return n; 54 if (n = 0) return m; 55 int t = m> n? N: m; 56 while (m % t | n % t) t --; 57 return t; 58} 59 int main (int argc, char * argv []) 60 {61 int m, n; 62 cout <"--- Euclidean Algorithm for Finding the maximum public approx ---" <endl; 63 cout <"Enter the first m :"; 64 cin> m; 65 cout <"enter the second number n:"; 66 cin> n; 67 int result; 68 result = Gcd1 (m, n ); 69 cout <"Gcd1 Function Calculation result:" <result <endl; 70 result = Gcd2 (m, n); 71 cout <"Gcd2 function calculation result: "<result <endl; 72 result = Gcd3 (m, n); 73 cout <" Gcd3 Function Calculation result: "<result <endl; 74}
2. Add the statements for calculating the number of operations in Gcd1, Gcd2, and Gcd3 to estimate which one is faster and how many times the fastest and slowest difference is.
Part 2 positive integer sequence output in reverse order
Program problems:
1. Enter a 1234567890 reverse order.
2. Enter a 4294967296 reverse order.
1 # include <iostream. h> 2 // the positive integer sequence is output in reverse order 3 4 void PrintDigit (int n) {5 cout <n % 10; 6 if (n> = 10) {7 PrintDigit (n/10); 8} 9} 10 11 int main () {12 int n; 13 cin> n; 14 PrintDigit (n); 15}
Part 3 tower of Hanoi
Program problems:
1. Sequence of moving the three plates:
The disk |
|
Is moved from |
|
To top of tower |
|
The disk |
|
Is moved from |
|
To top of tower |
|
The disk |
|
Is moved from |
|
To top of tower |
|
The disk |
|
Is moved from |
|
To top of tower |
|
The disk |
|
Is moved from |
|
To top of tower |
|
The disk |
|
Is moved from |
|
To top of tower |
|
The disk |
|
Is moved from |
|
To top of tower |
|
2. If five plates are moved, () is required.
1 # include <stdio. h> 2 // the first tower is the initial Tower, the middle tower is the borrow tower, and the last tower is the target Tower 3 int I = 1; // record Step 4 void move (int n, char from, char to) // move the plate number n from to 5 {printf ("Step % d: % d plate % c ----> % c \ n ", I ++, n, from, to); 6} 7 void hanoi (int n, char from, char denpend_on, char to) // move n plates from initial tower to target tower (borrow Tower) 8 {9 if (n = 1) 10 move (1, from, to); // only one plate is to directly move the plate on the first tower to the destination 11 else12 {13 hanoi (n-1, from, to, denpend_on ); // first move n-1-1 plates of the initial tower to the borrow tower with the help of the target Tower 14 move (n, from, ); // move the remaining plate to the target tower 15 hanoi (n-1, denpend_on, from, ); // Finally, move the n-1 plates on the borrow tower to the destination tower 16} 17} 18 int main () 19 {20 printf ("Enter the number of plates: \ n "); 21 int n; 22 scanf ("% d", & n); 23 char x = 'A', y = 'B', z = 'C '; 24 printf ("plate movement: \ n"); 25 hanoi (n, x, y, z); 26}
Iv. Experiment Summary