What is the method to use C language recursive function to solve the problem of 5?

What is the method to use C language recursive function to solve the problem of 5?

In C programming, recursion is a very useful technique that simplifies problem solving and improves code reusability. This article will take solving the number 5 as an example to introduce how to use C language recursive functions to achieve this task.

9. What is the method to use C language recursive function to solve the problem of 5?

First, let's clearly define the problem. The method of solving the number 5 is to obtain the expression of the number 5 in a given set of numbers through the combination of addition, subtraction, multiplication, division and parentheses. For example, from the set of numbers {1, 2, 3, 4}, the number 5 can be obtained by the following expression: 2 + 3 = 5.

To solve this problem, we first need to define a recursive function that will return all expressions that satisfy the condition based on a given set of numbers and a target number.

void solve(int* nums, int n, int target, char* expr, int sum, int last, int index) {

if (index == n) { // Recursive termination condition

if (sum == target) {

printf(\s\

\ expr);

}

return;

}

// Addition: Add the current number to the expression

int len = strlen(expr);

expr[len] = '+';

expr[len + 1] = ‘0’ + nums[index];

expr[len + 2] = '\0';

solve(nums, n, target, expr, sum + nums[index], nums[index], index + 1);

expr[len] = '\0';

// Subtraction: Subtract the current number to the expression

len = strlen(expr);

expr[len] = '-';

expr[len + 1] = ‘0’ + nums[index];

expr[len + 2] = '\0';

solve(nums, n, target, expr, sum - nums[index], -nums[index], index + 1);

expr[len] = '\0';

// Multiplication: Multiply the current number into the expression

len = strlen(expr);

expr[len] = '*';

expr[len + 1] = ‘0’ + nums[index];

expr[len + 2] = '\0';

solve(nums, n, target, expr, sum - last + last * nums[index], last * nums[index], index + 1);

expr[len] = '\0';

// Division: Divide the current number into the expression

len = strlen(expr);

expr[len] = '/';

expr[len + 1] = ‘0’ + nums[index];

expr[len + 2] = '\0';

solve(nums, n, target, expr, sum - last + last / nums[index], last / nums[index], index + 1);

expr[len] = '\0';

//Do not use current number

solve(nums, n, target, expr, sum - last, last, index + 1);

}

The above is the implementation of a recursive function. Its input parameters include the number set nums, the number of numbers in the set n, the target number target, the current expression expr, the current number and sum, the previous number last, and the index index of the current number.

In a recursive function, we first check the recursion termination condition, which is whether the sum of the expression is equal to the target number when all numbers have been considered. If so, we print the expression.

Next, we add the current number to the expression using the addition, subtraction, multiplication, and division operators, and update the sum of the expression, the previous number, and the index of the current number by recursively calling the next level function.

Finally, we also need to handle the case where the current number is not used and update the index of the current number by calling the function recursively.

In the main program, we define a number set {1, 2, 3, 4} and call the recursive function to solve the problem of solving the number 5.

int main() {

int nums[] = {1, 2, 3, 4};

int n = sizeof(nums) / sizeof(nums[0]);

int target = 5;

char expr[50];

solve(nums, n, target, expr, 0, 0, 0);

return 0;

}

By running the above program, we will get all the expressions that satisfy the conditions, that is, through the combination of addition, subtraction, multiplication, division and parentheses, we will get the expression of the number 5.

Through the above code and examples, we can clearly understand the method of using C language recursive functions to solve the problem of 5. Recursive functions handle different situations by continuously calling themselves to get a solution to the problem. This approach not only simplifies the problem-solving process, but also improves code reusability. To solve problems with other numbers, you only need to modify the number set and target number.

To sum up, with the help of C language recursive functions, we can solve various complex problems more conveniently and improve the readability and maintainability of the code. Mastering recursive technology will bring more possibilities to our C language programming development.
Part of the code is transferred from: https://www.wodianping.com/c/2023-08/254369.html

Guess you like

Origin blog.csdn.net/qq_42151074/article/details/132270751