当前位置:网站首页>Exercises of function recursion
Exercises of function recursion
2022-07-03 19:52:00 【-Taco-】
// According to the following recursive function : Call function Fun(2), What is the return value ( )
int Fun(int n)
{
if(n==5)
return 2;
else
return 2*Fun(n+1);
}
Fun(2)---> return 16
return 2*Fun(3) 2*8=16
|__Fun(3):8
return 2*Fun(4) 2*4=8
|__Fun(4):4
return 2*Fun(5) 2*2=4
|__Fun(5):2
return 2
// Recursively print each bit of an integer
#include<stdio.h>
void print(int n)
{
if (n > 9)
{
print(n / 10);
}
printf("%d ", n % 10);
}
int main()
{
int num = 0;
scanf("%d", &num);
print(num);
return 0;
}
// Recursion and non recursion are used to calculate respectively n The factorial ( Overflow is not considered )
#include<stdio.h>
int func(int i)
{
if (i < 2)
{
return 1;
}
else
{
return i * func(i - 1);
}
}
int main()
{
int n = 0;
scanf("%d", &n);
int ret = func(n);
printf("%d", ret);
return 0;
}
#include<stdio.h>
int main()
{
int n = 0;
scanf("%d", &n);
int sum = 1;
int i = 1;
for ( i = 1; i <= n; i++)
{
sum *= i;
}
printf("%d", sum);
return 0;
}
// Recursive and non recursive are implemented respectively strlen
#include<stdio.h>
//strlen The simulation ( Recursive implementation )
int func(char* arr)
{
if (*arr != '\0')
{
return 1 + func(arr + 1);
}
else
{
return 0;
}
}
int main()
{
char arr[] = "abcde";
int ret=func(arr);
printf("%d", ret);
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
char arr[] = "abcde";
int ret = strlen(arr);
printf("%d", ret);
return 0;
}
strlen The meaning is : Find the length of valid characters in the string , barring \0
Cycle to achieve :
1. Give a count , Used to count the number of valid characters
2. Traversal string , As long as you don't meet \0, When a character is encountered, add... To the count 1, Until I met \0
// Write a function reverse_string(char * string)( Recursive implementation )
// Realization : Invert the characters in the parameter string , Not in reverse order .
// requirement : Out of commission C String manipulation functions in the function library .
#include<stdio.h>
void reverse_string(char* arr)
{
int len = strlen(arr);
char tmp = *arr;
*arr = *(arr + len - 1);
*(arr + len - 1) = '\0';
if (strlen(arr + 1) >= 2)
reverse_string(arr + 1);
*(arr + len - 1) = tmp;
}
int main()
{
char arr[] = "abcde";
reverse_string(arr);
printf("%s", arr);
return 0;
}

Ideas : |
Inverse string , The implementation of the loop is very simple |
1. Give two pointers ,left Put to the left of the string ,right Put in the last valid character position |
2. Swap characters at two pointer positions |
3. left The pointer goes back ,right The pointer goes forward , As long as the two pointers don't meet , continue 2, After the two pointers meet , Reverse end |
*/ |
void reverse_string(char* arr) |
{ |
char *left = arr; |
char *right = arr+strlen(arr)-1; |
while(left<right) |
{ |
char tmp = *left; |
*left = *right; |
*right = tmp; |
left++; |
right--; |
} |
} |
/* |
recursively : |
For strings “abcdefg”, The general principle of recursive implementation : |
1. In exchange for a and g, |
2. Recursively inverts the rest of the source string , The rest can be treated as a valid string , And reverse it in a similar way |
*/ |
void reverse_string(char* arr) |
{ |
intlen = strlen(arr); |
char tmp = *arr; |
*arr = *(arr+len-1); |
*(arr+len-1) = '\0'; |
if(strlen(arr+1)>=2) |
reverse_string(arr+1); |
*(arr+len-1) = tmp; |
} |
// Write a recursive function DigitSum(n), Enter a non negative integer , Returns the sum of the numbers that make up it
// for example , call DigitSum(1729), You should go back to 1+7+2+9, Its sum is 19
// Input :1729, Output :19
#include<stdio.h>
int func(int a)
{
if (a > 9)
{
return a%10+func(a / 10);
}
else
{
return a;
}
}
int main()
{
unsigned int a = 0;
scanf("%d", &a);
int ret = func(a);
printf("%d", ret);
return 0;
}

// Write a function to implement n Of k Power , Use recursion to implement .
#include<stdio.h>
int func(int a, int b)
{
if (b == 0)
return 1;
else if (b >= 1)
{
return a * func(a, b - 1);
}
}
int main()
{
int a = 0;
int b = 0;
scanf("%d %d", &a, &b);
int ret = func(a, b);
printf("%d", ret);
return 0;
} 
// Recursion and non recursion are implemented respectively n Fibonacci Numbers
#include<stdio.h>
int func(int n)
{
if (n <= 2)
{
return 1;
}
else
{
return func(n-1) + func(n - 2);
}
}
int main()
{
int n = 0;
scanf("%d", &n);
int ret = func(n);
printf("%d", ret);
return 0;
}

#include<stdio.h>
int fib(int n)
{
int result;
int pre_result;
int next_older_result;
result = pre_result = 1;
while (n > 2)
{
n -= 1;
next_older_result = pre_result;
pre_result = result;
result = pre_result + next_older_result;
}
return result;
}
int main()
{
int n = 0;
scanf("%d", &n);
int ret = fib(n);
printf("%d", ret);
return 0;
} 
边栏推荐
- 论文阅读 GloDyNE Global Topology Preserving Dynamic Network Embedding
- Gym welcomes the first complete environmental document, which makes it easier to get started with intensive learning!
- 05 -- QT OpenGL draw cube uniform
- Wechat applet quick start (including NPM package use and mobx status management)
- Vscode reports an error according to the go plug-in go get connectex: a connection attempt failed because the connected party did not pro
- Win10 share you don't have permission
- Chapter 2: find the number of daffodils based on decomposition, find the number of daffodils based on combination, find the conformal number in [x, y], explore the n-bit conformal number, recursively
- Day11 - my page, user information acquisition, modification and channel interface
- HCIA-USG Security Policy
- The space of C disk is insufficient, and the computer becomes stuck. Quickly expand the capacity of C disk to make the system more smooth
猜你喜欢

2022 Xinjiang latest road transportation safety officer simulation examination questions and answers

第一章:求所有阶乘和数,大奖赛现场统分程序设计,三位阶乘和数,图形点扫描,递归求n的阶乘n!,求n的阶乘n!,舍罕王失算

Flume learning notes

PR 2021 quick start tutorial, material import and management

2022-06-30 网工进阶(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】

第一章:求同吗小数和s(d, n)

PR FAQ: how to set PR vertical screen sequence?

IPv6 experiment

Chapter 1: drinking soft drinks, step tariff calculation, step tariff calculation function, personal income tax, solving square root inequality, simplifying solving square root inequality, solving dem

第一章:喝汽水,阶梯电费计算,阶梯电费计算函数,个人所税,求解平方根不等式,简化求解平方根不等式,求解调和级数不等式,解不等式:d<1+1/2-1/3+1/4+1/5-1/6+..士1/n
随机推荐
Ae/pr/fcpx super visual effects plug-in package fxfactory
Make a simple text logo with DW
[raid] [simple DP] mine excavation
unittest框架基本使用
Typora charges, WTF? Still need support
Meso tetra [P - (p-n-carbazole benzylidene imino)] phenylporphyrin (tcipp) /eu (tcipp) [pc( α- 2-oc8h17) 4] and euh (tcipp) [pc (a-2-oc8h17) 4] supplied by Qiyue
Gym welcomes the first complete environmental document, which makes it easier to get started with intensive learning!
Chapter 2: 4-digit Kaplan number, search even digit Kaplan number, search n-digit 2-segment sum square number, m-digit ingenious square number without 0, specify the number to form a 7-digit square nu
Class loading process
Geek Daily: the system of monitoring employees' turnover intention has been deeply convinced off the shelves; The meta universe app of wechat and QQ was actively removed from the shelves; IntelliJ pla
Win10 share you don't have permission
Micro service knowledge sorting - three pieces of micro Service Technology
Buuctf's different flags and simplerev
Realize user registration and login
第一章:简化同码小数和s(d, n)
第一章:拓广同码小数和s(d, n)
Part 28 supplement (XXVIII) busyindicator (waiting for elements)
Read the paper glodyne global topology preserving dynamic network embedding
[effective Objective-C] - block and grand central distribution
Don't be afraid of no foundation. Zero foundation doesn't need any technology to reinstall the computer system