The idea of using recursive functions to solve problems is called recursion;
Steps to write a recursive function: 1. Write a statement to solve each small problem after decomposition (assuming the recursive function is already available) 2. Write a branch at the beginning of the recursive function to solve the situation that cannot be decomposed (this branch must ensure that the function can end)
#include"stdio.h" #include"stdlib.h" // take a break every 100 meters, a total of 7 breaks int step(int m,int times) { if(times == 0){} else{ m = m+100; return step(m,times-1); } } //Sell half and one duck every time you pass a village int q(int duck,int village) { if(village==0) { } else { duck = (duck+1)*2; return q(duck,village-1); } } int main(void) { //Walk through 7 villages, there are 2 ducks left, how many ducks are there in total int duck_leave = 2; int village = 7; int total_duck = q(2,7); printf("%d\n",total_duck); //After 7 breaks, there are 50 steps left, find the total distance int m = step(50,7); printf("%d\n",m); return 0; }