C++ recursion problems explain factorial, Fibonacci sequence, output asterisk, Yang Hui triangle

Recursion is a programming concept used in function definitions, which means that a function can call itself. Recursive functions must have a base condition (or termination condition) under which recursive calls will stop. Without this termination condition, the recursion will continue indefinitely, eventually causing the program to crash.

In programming, recursion is often used to solve problems that can be broken down into smaller, simpler sub-problems. A recursive function breaks a large problem into smaller sub-problems and then calls itself to solve those sub-problems.

C++ code example to calculate factorial:

In this example,factorial() the function is a recursive function. When n is equal to 0, the function returns 1, which is the termination condition of the recursion. Otherwise, the function calls itself recursively, calculating the factorial of n . Each recursive call will decrement n by 1 until n equals 0. 

#include <iostream>  
  
int factorial(int n) {  
    if (n == 0) {  // 终止条件  
        return 1;  
    } else {  // 递归调用  
        return n * factorial(n - 1);  
    }  
}  
  
int main() {  
    int n;  
    std::cout << "请输入一个正整数:";  
    std::cin >> n;  
    std::cout << n << "的阶乘是:" << factorial(n) << std::endl;  
    return 0;  
}

 Code to implement Fibonacci sequence using C++:

In this program, we define a recursive function fibonacci to calculate each term of the Fibonacci sequence. If n is less than or equal to 1, the function directly returns n. Otherwise, the function recursively calls fibonacci(n-1) and fibonacci(n-2) to calculate the next item, and then adds the two values ​​to get the value of the next item. In the main function, we first enter the number of terms in the Fibonacci sequence to be calculated, and then use a loop to iterate through each term and output them. 

#include <iostream>  
  
int fibonacci(int n) {  
    if (n <= 1) {  
        return n;  
    } else {  
        return fibonacci(n-1) + fibonacci(n-2);  
    }  
}  
  
int main() {  
    int n;  
    std::cout << "请输入要计算的斐波那契数列的项数:";  
    std::cin >> n;  
    std::cout << "斐波那契数列的前" << n << "项为:" << std::endl;  
    for (int i = 0; i < n; i++) {  
        std::cout << fibonacci(i) << " ";  
    }  
    std::cout << std::endl;  
    return 0;  
}

C++ program that uses recursion to output asterisks:

#include <iostream>  
  
void printStars(int n) {  
    if (n == 0) {  // 递归终止条件  
        return;  
    }  
    std::cout << "*";  // 输出一个星号  
    printStars(n - 1);   // 递归调用函数  
}  
  
int main() {  
    int n;  
    std::cout << "请输入要输出的星号行数:";  
    std::cin >> n;  
    printStars(n);  
    return 0;  
}

 In the above program, the printStars() function uses recursion to output the asterisks for the specified number of lines. When n equals 0, the recursion terminates. Otherwise, the program outputs an asterisk and calls the printStars() function recursively, decrementing n by 1 each time until n is equal to 0. In the main() function, the program reads the number of asterisk lines to be output from the standard input, and calls the printStars() function to output the specified number of lines of asterisks.

Solving Yang Hui Triangle

The solution to this type of problem can be transitioned from Figure 1 to Figure 2. Figure 2 is Figure 1 with an extra space after each number and an extra space before each line.

It can be seen from the analysis on the right side of the picture. Recursive functions and termination conditions. i is the row and j is the column. In the recursive function, n is the row and m is the column.

As shown in Figure 1 below. is a simple Yang Hui triangle illustration.

 Figure 1 Program solution. The procedure is as follows:

#include <iostream>
using namespace std;
int y(int n,int m)
{
	if(m==1||m==n){
		return 1;
	}
	return y(n-1,m-1)+y(n-1,m);
}

int main() {
int m, n;
cin>>n;
for(int i=1;i<=n;i++){
	for(int j=1;j<=i;j++){
		
		cout<<y(i,j)<<" ";
	}
	cout<<"\n";
}
return 0;
}


Enter 5. operation result:

 as shown in picture 2. is a complex Yang Hui triangle illustration.

The procedure is as follows:

#include <bits/stdc++.h>
using namespace std;
int y(int n,int m)
{
	if(m==1||m==n){
		return 1;
	}
	return y(n-1,m-1)+y(n-1,m);
}

int main() {
int n;
cin>>n;
for(int i=1;i<=n;i++){
	for(int k=n-i;k>=1;k--){
				cout<<"   ";
			}
	for(int j=1;j<=i;j++){
		
		cout<<setw(5)<<y(i,j)<<"  ";
	}
	cout<<"\n";
}
return 0;
}


 Enter 20. operation result:

 

Guess you like

Origin blog.csdn.net/babyai996/article/details/134799076