C language exercises (recursion)

Table of contents

1. Accept an integer value (unsigned), and print each bit of it in order.

2. Writing a function does not allow the creation of temporary variables to find the length of the string.

3. Find the factorial of n. (overflow is not considered)

4. Find the nth Fibonacci number. (overflow is not considered)

5. Write a function reverse_string(char * str) (recursive implementation)

6. Write a recursive function DigitSum(n), input a non-negative integer, and return the sum of the numbers that make it up

7. Write a function to realize n to the kth power, and use recursion to realize it.


1. Accept an integer value (unsigned), and print each bit of it in order.

For example:
input: 1234, output 1 2 3 4

Idea: What we should think about is that the print function can print out every bit. This leads to the following series of thoughts.
print(1234)
print(123) 4 First print out each digit of 123, and then print 4.
print(12) 3 4 First print out each digit of 12, and then print 3. At print 4.
print(1) 2 3 4  ……

#include <stdio.h>

void print(int n)
{
	if (n > 9)
	{
		print(n/10);
	}
	printf("%d ", n % 10);


}

int main()
{
	unsigned int num = 0;
	scanf("%d", &num);//1234
	print(num);
	return 0;
}

2. Writing a function does not allow the creation of temporary variables to find the length of the string.

Our function requires the length of the string every time, and the parameter passed in is only the first address of the string, then we will calculate the length of the first character, and then recurse one by one

my_strlen("abcd")     
1 + my_strlen("bcd")
1+1+my_strlen("cd")
1+1+1+my_strlen("d")
1+1+1+1+my_strlen("")

int my_strlen(char* str)
{
	if (*str != '\0')
	{
		return 1 + my_strlen(str+1);
	}
	else
		return 0;
}

int main()
{
	char arr[] = "abcd";
	int len = my_strlen(arr);
	//len = strlen(arr);
	printf("%d\n", len);
	
	return 0;
}

3. Find the factorial of n. (overflow is not considered)

Idea: the factorial of n = n*(n-1)*(n-2)........*1; and the fac function we defined is to find the factorial, so just pass the value of n each time Just go in.

int fac(int n)
{
	if (n <= 1)
		return 1;
	else
		return n * fac(n - 1);
}

int main()
{
	int n = 0;
	scanf("%d", &n);
	int ret = fac(n);
	printf("%d ",ret);
	
	return 0;
}

4. Find the nth Fibonacci number. (overflow is not considered)

Idea: Fibonacci sequence: 1 1 2 3 5 8 13 21 34 55... And we can write mathematical expressions based on this characteristic. Then write the code.

int fib(int n)
{
	if (n <= 2)
		return 1;
	else
		return fib(n - 1) + fib(n - 2);
}

int main()
{
	int n = 0;
	scanf("%d", &n);
	int ret = fib(n);
	printf("%d ", ret);
	return 0;
}

5. Write a function reverse_string(char * str) (recursive implementation)

Implementation: Reverse the characters in the parameter string instead of printing them in reverse order.

Requirement: The string manipulation functions in the C function library cannot be used.

Example: "abcdef" becomes "fedcba"

Idea: Every time the reverse_string function is called, the first and last two characters of the string are exchanged, but it should be noted that the last character needs to be filled with the character '\0', because the end position of the new string needs to be obtained.

#include <stdio.h>
#include <string.h>

void reverse_string(char* str)
{
	int len = strlen(str);
	char tmp = *str;
	*str = *(str + len - 1);
	*(str + len - 1) = '\0';

	if (strlen(str + 1) >= 2)
		reverse_string(str + 1);

	*(str + len - 1) = tmp;
}

int main()
{
	char arr[] = "abcdef";
	reverse_string(arr);

	printf("%s\n", arr);
	return 0;
}

6. Write a recursive function DigitSum(n), input a non-negative integer, and return the sum of the numbers that make it up

For example, calling DigitSum(1729) should return 1+7+2+9, which sums to 19

Input: 1729, Output: 19

Idea: The idea is roughly the same as the first question.

#include <stdio.h>

int DigitSum(int n)
{
	if (n > 9)
    	return (n % 10) + DigitSum(n / 10);
	else
		return n;
}

int main()
{
	int num = 0;
	scanf("%d", &num);
	int ret = DigitSum(num);
	printf("%d\n", ret);
	return 0;
}

7. Write a function to realize n to the kth power, and use recursion to realize it.

Idea: Just pay attention to the value of k. 

double power(int n, int k)
{
	if (k > 0)
		return n * power(n,k - 1);
	else if(k == 0)
		return 1;
	else
		return 1.0 / power(n, -k);
}

int main()
{
	int n = 0;
	int k = 0;
	scanf("%d %d", &n,&k);
	double ret = power(n, k);
	printf("%.1f\n", ret);
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_63562631/article/details/125558351