Algorithm Design and Analysis-Chapter 2 Recursive Algorithm

The characteristics of the recursive method are: a problem requires a series of calculations. There is always a certain relationship between the known conditions and the problem. When calculating, if the quantitative relationship between the before and after processes can be found (That is, recursive), then, from the problem to gradually push to the known conditions, this method is called backstepping. Whether it is forward or backward, the key is to find a recursive formula.
[ Example 1 ] Digital triangle. The figure below shows a digital triangle. Please compile a program to calculate a path from top to bottom somewhere, so that the sum of the numbers passed by the path is the largest. Only the total sum is required.  
1. One step can go down the left oblique line or right oblique line;  
2. The number of triangle lines is less than or equal to 100;
3. The numbers in the triangle are 0, 1, ..., 99; The test data is input line by line through the keyboard The data in the above example should be entered in the format shown below:
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Insert picture description here
[Algorithm analysis] There are many solutions to this problem, starting from the recursive idea, imagine that when the top When walking along a certain path to the i-th layer and advancing to the i + 1-th layer, our choice must be to advance along the direction of the largest number in the next two feasible paths. To this end, we can use the method of backward push, set a [i] [j] stores the maximum value starting from i, j and reaching the nth layer, then a [i] [j] = max {a [i] [j] + a [i + 1] [j], a [ i] [j] + a [i + 1] [j + 1]}, a [1] [1] is the maximum value of the sum of the numbers.

#include<iostream> 
using namespace std; 
int main() 
{   
	int n,i,j,a[101][101];   
	cin>>n;
	for (i=1;i<=n;i++)    
		for (j=1;j<=i;j++)      
			cin>>a[i][j];                             
	for (i=n-1;i>=1;i--)    
		for (j=1;j<=i;j++)
		{      
			if (a[i+1][j]>=a[i+1][j+1]);
				a[i][j]+=a[i+1][j];    
			else  
				a[i][j]+=a[i+1][j+1];      
		}    
		cout<<a[1][1]<<endl;  
}

[ Example 2 ] The sequence that satisfies F1 = F2 = 1, Fn = Fn-1 + Fn-2 is called the Fibonacci sequence (Fibonacci), and the first few items are 1, 1, 2, 3, 5, 8 , 13, 21, 34 ... Find the nth term of this sequence (n> = 3).
That is: f1 = 1 (n = 1)
f2 = 1 (n = 2)
fn = fn-1 + fn-2 (n> = 3)

#include<iostream> 
#include<cstdio> 
using namespace std; 
int main() 
{     
	int f0=1,f1=1,f2;     
	int n;     
	cin>>n;
	for (int i=3; i<=n; ++i)     
	{          
		f2=f0+f1;          
		f0= f1;          
		f1=f2;     
	} 
	printf("%d\n",f2); 
	return 0; 
} 

[ Example 3 ] There is a rectangular square of 2χn, and the square is covered with a 1 * 2 domino.
Insert picture description here
Write a program, try any one of the given n (n> 0), and output the total number of paving methods.
The following is a c ++ program that inputs n and outputs x1 ~ xn:

#include<iostream> 
using namespace std; 
int main() 
{   
	int n,i,j,a[101];   
	cout<<"input n:";                       
	cin>>n;
	a[1]=1;a[2]=2;   
	cout<<"x[1]="<<a[1]<<endl;   
	cout<<"x[2]="<<a[2]<<endl;   
	for (i=3;i<=n;i++)                   
	{
		a[i]=a[i-1]+a[i-2];      
		cout<<"x["<<i<<"]="<<a[i]<<endl;     
	} 
} 

[ Example 4 ] Insect reproduction
[Problem description] Scientists have discovered a special kind of insect in tropical forests. This kind of insect has strong reproduction ability. Each pair of adults lays y pairs of eggs after x months, and each pair of eggs grows into adult adults after two months. Assuming that each adult does not die, there is only one pair of adults in the first month, and the egg does not lay eggs in the first month after the adult grows (laying eggs after X months), how many pairs of adults are there after Z months? 0 = <X <= 20,1 <= Y <= 20, X = <Z <= 50
[input format] The value of x, y, z
[output format] After Z months, the total number of adult worms
[input Sample] 1 2 8
[Sample output] 37

#include<iostream> 
using namespace std; 
int main() 
{   
	long long a[101]={0},b[101]={0},i,j,x,y,z;   
	cin>>x>>y>>z;
	for(i=1;i<=x;i++){a[i]=1;b[i]=0;}   
		for(i=x+1;i<=z+1;i++)              
		{     
			b[i]=y*a[i-x];     
			a[i]=a[i-1]+b[i-2];             }
		cout<<a[z+1]<<endl;   
		return 0; 
}		

[ Example 5 ] Number of digits problem
[Problem description] Among all N digits, how many of them have an even number of 3? Since the result may be large, you only need to output the value of the answer to 12345.
[Input format] Read in a number N
[Output format] There are even numbers 3 in the output.
[Sample input] 2
[Sample output] 73
[Data size] 1 <= N <= 1000
[Sample description] Among all 2 digits, there are 72 including 0 3, including 2 3 There are 1 of 73

#include<iostream> 
using namespace std; 
int main() 
{   
	int f[1001][2],n,i,x;   
	cin>>n;
	f[1][1]=1;f[1][0]=9;                           
	for(i=2;i<=n;i++)     
	{          
		x=f[1][0];       
		if(i==n)
			x--;
		f[i][0]=(f[i-1][0]*x+f[i-1][1])%12345;       
		f[i][1]=(f[i-1][1]*x+f[i-1][0])%12345;       
	}    
	cout<<f[n][0];     
	return 0; 
}
Published 48 original articles · Like 25 · Visit 2453

Guess you like

Origin blog.csdn.net/qq_43628959/article/details/105061314