1. Euclidean recursion algorithm
2. Euclid Iterative Algorithm
3. Continuous integer detection algorithm
4. Recursive algorithm
Experiment One The basis of algorithm problem solving
Experiment name: The basis of algorithm problem solving
Experimental chapters: Algorithm design and analysis Chapter one content
Experimental content
The first part Euclidean algorithm to seek greatest common divisor
Problem:
1. Calculation: 34931 and 75236 greatest common divisor:
1#include <iostream.h>2 3 //1. Euclidean recursion algorithm4 voidSwap (int&a,int&b)5 {6 intc=a;a=b;b=C;7 }8 9 intRGCD (intMintN)Ten { One if(m==0) A returnN; - returnRGCD (n%m,m); - } the - intGCD1 (intMintN//Control procedure 1-1 - { - + if(m>N) - { + Swap (m,n); A } at returnRGCD (m,n); - } - - - //2. Euclid Iterative Algorithm - intGCD2 (intMintN//Control procedure 1-2 in { - if(m==0) to { + returnN; - } the if(n==0) * { $ returnm;Panax Notoginseng } - if(m>N) the { + Swap (m,n); A } the while(m>0){ + intc=n%m;n=m;m=C; - } $ returnN; $ } - - //3. Continuous integer detection algorithm the - intGCD3 (intMintN//Control procedure 1-3Wuyi { the //Student Input Program Section - if(m==0)returnN; Wu if(n==0)returnm; - intt = m>n?n:m; About while(m%t | | n%t) t--; $ returnT; - } - intMainintargcChar*argv[]) - { A intM,n; +cout<<"---Euclidean algorithm for greatest common divisor---"<<Endl; thecout<<"Please enter the first number m:"; -Cin>>m; $cout<<"Please enter a second number n:"; theCin>>N; the intresult; theresult =GCD1 (m,n); thecout<<"The result of the GCD1 function operation is:"<<result<<Endl; -result =GCD2 (m,n); incout<<"The result of the GCD2 function operation is:"<<result<<Endl; theresult =GCD3 (m,n); thecout<<"The result of the GCD3 function operation is:"<<result<<Endl; About}
2. Increase the number of calculation operations in GCD1, GCD2, GCD3, and estimate which one is faster, the fastest and the slowest the number of times.
Second part inverse output positive integer sequence
Procedural issues:
1. Enter a 1234567890 to reverse the order.
2. Enter a 4294967296 to reverse the order.
1#include <iostream.h>2 //reverse output positive integer sequence3 4 voidPrintdigit (intN) {5cout<< n%Ten;6 if(n>=Ten){7Printdigit (n/Ten);8 }9 }Ten One intMain () { A intN; -Cin>>N; - printdigit (n); the}
Part Three Hanoi tower problem
Procedural issues:
1. Moving order of 3 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 the 5 plates move, you need to complete the () step.
1#include <stdio.h>2 //The first tower for the initial tower, the middle tower for borrowing the tower, the last one tower for the goal tower3 intI=1;//record number of steps4 voidMoveintNChar from,CharRoo//The plate numbered n is moved from the from5{printf ("Step%d: Set%d plate%c---->%c\n", I++,n, from, to);6 }7 voidHanoiintNChar from,CharDENPEND_ON,CharRoo//move n plates from the initial tower to the target tower (using a borrowed tower)8 {9 if(n==1)TenMove1, from, to);//there's only one plate that moves the plates on the first tower to the destination. One Else A { -Hanoi (n1, from, to,denpend_on);//first, the first n-1 plates of the initial tower are moved to the borrowed tower with the purpose tower. -Move (N, from, to);//move the remaining plate to the destination tower. theHanoi (n1, denpend_on, from, to);//Finally, we'll borrow the n-1 plates from the tower and move them to the target tower . - } - } - intMain () + { -printf"Please enter the number of plates: \ n"); + intN; Ascanf"%d",&n); at Charx='A', y='B', z='C'; -printf"The plate moves as follows: \ n"); - Hanoi (n,x,y,z); -}
Experimental algorithm problem solving basis-Euclidean recursive algorithm and recursive algorithm