Jilin University Class 2020 Superstar High-level Language Programming Homework Experiment 06 Recursive Programming

Hope to discuss with you.

1 Subject name: permutation and combination

Problem description: Write a program to find the value of the function C(m,n).

Input: Randomly input a natural number and a non-negative integer from the keyboard as the value of m and n (m≥n).
Output: the value of the function C(m,n).

Example 1:
Input:
4 1
Output:
4

Example 2:
Input:
6 2
Output:
15

#include<stdio.h>
int c(int m, int n) {
    
    
	if (n < 0)
		return 0;
	else if (n == 0)
		return 1;
	else if (n == 1)
		return m;
	else if (m < 2 * n)
		return c(m, m - n);
	else
		return c(m - 1, n - 1) + c(m - 1, n);
}
int main() {
    
    
	int m, n;
	scanf("%d%d", &m, &n);
	printf("%d", c(m, n));
	return 0;
}

2Question name: Hermite polynomial

Topic description: Write a program to solve the Hermite polynomial value by recursive method. The Hermite polynomial is defined as follows.

Input: Randomly input a non-negative integer and a real number from the keyboard as the value of n and x.

Output: the value of H n (x), accurate to 2 decimal places.

Example 1:

Input:
0 1.5
Output:
1.00
Example 2:

Input:
2 2.4
Output:
21.04

#include<stdio.h>
double H(double n, double x) {
    
    
	if (n == 0)
		return 1;
	else if (n == 1)
		return 2 * x;
	else
		return 2 * x * H(n - 1, x) - 2 * (n - 1) * H(n - 2, x);
}
int main() {
    
    
	double n, x;
	scanf("%lf%lf", &n, &x);
	printf("%.2lf", H(n, x));
	return 0;
}

3 topic name: Ackerman function

Problem description: Write a program to calculate the value of the Ackerman function. The Ackerman function is defined as follows

Input: Randomly input two non-negative integers from the keyboard as the values ​​of m and n.

Output: the value of Ack(m, n).

Example 1: Input 2 3 Output 9

Example 2: Input 3 2 Output 29

Example 3: Input 0 3 Output 4

#include<stdio.h>
int Ack(int m, int n) {
    
    
	if (m == 0)
		return n + 1;
	else if (n == 0)
		return Ack(m - 1, 1);
	else if (m > 0 && n > 0)
		return Ack(m - 1, Ack(m, n - 1));
}
int main() {
    
    
	int m, n;
	scanf("%d%d", &m, &n);
	printf("%d", Ack(m, n));
	return 0;
}

4 Title of Question: Greatest Common Factor

Topic description: Write a program to solve the greatest common divisor of m and n by recursive method. For positive integers u and v, the Euclidean division algorithm can be used to find their greatest common factor. The specific process is as follows:

u% v → r 1

v % r1 → r2

r1% r2 → r3

r2 % r3 → r4

… …

rn-1% rn → rn+1=0

When the remainder r n+1 =0, the calculation process ends, and r n is the greatest common factor of the positive integers u and v.

Input: Randomly input two positive integers m and n from the keyboard. Output: the greatest common factor.

Example 1:

Input:
12 15
Output:
3
Example 2:

Input:
28 49
Output:
7

#include<stdio.h>
int gcd(int u, int v) {
    
    
	int r = u % v;
	if (r != 0)
		r = gcd(v, r);
	else return v;
}
int main() {
    
    
	int u, v;
	scanf_s("%d%d", &u, &v);
	printf("%d", gcd(u, v));
	return 0;
}

5 Title: Sequential search

Topic description: Write a program to search sequentially in integer groups by recursive method.

enter:

Enter a positive integer n (0<n≤100) in the first line, indicating the number of elements in the array;

Enter n integers in the second line as elements of the array;

Enter the keyword to be retrieved in the third line.

Output:

If there is a keyword in the array, output its first occurrence position (the position with a smaller subscript value) otherwise it outputs NULL.

Example 1:

Input:
8
0 2 3 4 5 9 10 8
3
Output:
2
Example 2:

Input:
8
0 2 3 4 5 9 10 8
6
Output:
NULL

#include<stdio.h>
int index(int* a, int n, int x, int i) {
    
    
	if (i < n) {
    
    
		if (*(a + i) == x) return i;
		else  return index(a, n, x, i + 1);
	}
	else  return 101;
}
int main() {
    
    
	int a[100], i, n, x;
	scanf_s("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%d", &a[i]);
	scanf("%d", &x);
	if (index(a, n, x, 0) == 101)
		printf("NULL");
	else
		printf("%d", index(a, n, x, 0));
	return 0;
}

This 101 is hard to explain in one word, please advise

6 Title: The largest element

Title description: Write a program to solve the maximum element value in an integer array of length n by recursive method.

Input: Enter a positive integer n (0<n≤100) in the first line, which represents the number of elements in the array; in the second line, enter n integers in turn as the elements of the array.

Output: the value of the largest element.

Example 1:

Input:
10
9 8 7 6 5 4 3 2 1 0
Output:
9
Example 2:

Input:
10
0 1 2 3 4 5 6 7 8 9
Output:
9

#include<stdio.h>
int maxele(int* a, int n, int tmp, int i) {
    
    
	if (i < n) {
    
    
		if (*(a + i) > tmp) tmp = *(a + i);
		return maxele(a, n, tmp, i + 1);
	}
	else return tmp;
}
int main() {
    
    
	int a[100], i, n;
	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%d", &a[i]);
	printf("%d", maxele(a, n, a[0], 0));
	return 0;
}

7Question name: Array reverse order

Topic description: Write a program to reverse the order of the array using the recursive method.

Input: Enter a positive integer n (0<n≤100) in the first line, which represents the number of elements in the array; in the second line, enter n integers in turn as the elements of the array.

Output: sequentially output the elements in the array in reverse order, separated by a space between the elements, and there is no character after the last element.

Example 1:

Input:
8
0 2 3 4 5 9 10 8
Output:
8 10 9 5 4 3 2 0
Example 2:

Input:
5
0 2 3 3 5
Output:
5 3 3 2 0

#include<stdio.h>
void reverse(int* a, int len) {
    
    
	if (len > 1) {
    
    
		int tmp = a[0];
		a[0] = a[len - 1];
		a[len - 1] = tmp;
		reverse(a + 1, len - 2);
	}
}
int main() {
    
    
	int n, a[100] = {
    
     0 }, i;
	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%d", &a[i]);
	reverse(a, n);
	printf("%d", a[0]);
	for (i = 1; i < n; i++)
		printf(" %d", a[i]);
	return 0;
}

8 Title of Topic: Cut Wooden Strips

Title description:

Given a wooden strip of length n, cut it at approximately 2/5 to obtain 2 wooden strips whose length is still an integer; if the length of the newly obtained wooden strip still exceeds the specified length k, it will continue to follow the above The method processes the obtained wooden strips until the length of all wooden strips is not greater than k.

Write a program to calculate a wooden strip of length n by recursive method. When the specified length is k, how many wooden strips will be obtained after the above truncation process. Among them: n and k are all positive integers, n>10, k>3, and assuming that the length of the short wooden strips obtained by truncating the wooden strips is rounded to a positive integer, the length of the long wooden strips is the total length minus the length of the short wooden strips.

Input: Input two positive integers n and k (n>10, k>3) sequentially from the keyboard.

Output: the number of sticks.

Example 1:

Input: 20 4
Output: 7
Example 2:

Input: 3 20
Output: 1

#include<stdio.h>
int CS(int n, int k) {
    
    
	if (n <= k)  return 1;
	else   return CS((int)(n * 2 / 5+0.5), k) + CS(n - (int)(n * 2 / 5+0.5), k);//这里将n*2/5四舍五入再送入
}
int main() {
    
    
	int n, k;
	scanf("%d%d", &n, &k);
	printf("%d", CS(n, k));
	return 0;
}

My thoughts: The understanding of recursion needs to be further strengthened. Welcome to discuss.

Guess you like

Origin blog.csdn.net/u010656213/article/details/110919530