C language programming every day (recursion: Fibonacci numbers, the story of the cow)

recursive condition

A recursive function must have a condition that can exit directly, otherwise it will enter infinite recursion, for example

#include<stdio.h>
void f(int n)
{
	if(n<0)
		return;
	f(n-1);
	printf("%d ",n);
}

int main()
{
	int n=5;
	f(n);
	return 0;
	
}
 

//recursive exit
if(n<0)
        return;

The calling process is shown in the figure

Note:

• When calling recursively, the stack memory of the function changes as shown in the figure below. It can be seen that as the recursive function goes deeper, the stack space gradually grows downward. If the recursive level is too deep, it is easy to run out of stack memory.
• When the problem is advanced layer by layer, the scale of the problem will be reduced accordingly, and when it is reduced to the condition that can be directly exited, the function will start to return layer by layer.

 Let's see some exercises

topic description

There was a cow who gave birth to a heifer at the beginning of each year. Each heifer starts from the fourth year and also gives birth to a heifer at the beginning of each year. Please program to realize how many cows there are in the nth year?

input format

The input data consists of multiple test cases, each test case occupies one line, including an integer n (0<n<55), and the meaning of n is as described in the title.
n=0 means the end of the input data, no processing.

output format

For each test instance, output the number of cows in year n.
Each output occupies one line.

sample input

2
4
5
0

sample output

2
4
6

Based on the birth of a heifer every four years, the table can be listed:

years 1 2 3 4 5 6 7 8 9
number of cows 1 2 3 4 6 9 13 19 28

Observing the law to get

 code show as below

# include<stdio.h>
 
int fun(int n)
{
    if(n<=3) return n;
    else return fun(n-1)+fun(n-3);
}
int main()
{
    int n;
    while(scanf("%d",&n) && n)
/*
如果n不为0,则进入循环,也可以写为
while(treue)
{
    scanf("%d",&n);
    if(n==0)
        break;
}
*/
        printf("%d\n",fun(n));
 
    return 0;
}

similar to fibonacci numbers

1, 1, 2, 3, 5, 8, 13, 21, 34...
The law of the Fibonacci sequence is that, except for the first two items, the value of any other item is equal to the sum of the first two items. And the first two items are 1. Expressed in a mathematical formula is:

#include <stdio.h>

int fibonacci(int num)
{
    // 第1和第2个斐波那契数不需要经过任何计算,规定都是1
    if(num == 1 || num == 2)
        return 1;
    
    // 第N个斐波那契数,等于第N-1个和第N-2个斐波那契数之和
    return (fibonacci(num-1) + fibonacci(num-2));
}


int main(void)
{
  
    int num;
    scanf("%d", &num); 
    printf("第 %d 个斐波那契数是: %d\n", num, fibonacci(num));
    return 0;
}

Write a function that, given a base x and a power function N, computes x^{N}, for example

float ans1 = myPower(4, 2);     // 4的2次方
float ans2 = myPower(3.14, -2); // 3.14的-2次方

use recursion

x^{N}=x^{N-1}*x

The available code is as follows

#include<stdio.h>

 float f(float x,int n)
 {
 	if(n==0)
 		return 1;
 	else
 		return f(x,n-1)*x;
 }
 
 int main()
 {
 	float x;
 	int n;
 	scanf("%f %d",&x,&n);
 	if(x==0)
 		printf("0");
 		
    //除0之外的任何数的0次幂=1 
	else if(x != 0 && n == 0)
		printf("%f^%d = 1\n", x, n);
	
	//负整数次幂 
	else if(n<0)
	{
		n*=-1;
		printf("%f^-%d=%.6f",x,n,1/f(x,n));
	}
	
	//正整数次幂 
	 else
    {
        printf("%f^%d = %.6f\n", x, n, f(x, n));
    }
	return 0;
 }

 
 

example result

 Summarize

1. Before writing recursive code, be sure to observe the relationship between f(n) and f(n-1) to judge whether it is suitable to write recursively

2. Find the recursive exit, that is, the launch condition

If the intermediate code or expression is wrong, please feel free to enlighten me! !

Guess you like

Origin blog.csdn.net/weixin_69884785/article/details/131967022