当前位置:网站首页>C language exercises (recursion)
C language exercises (recursion)
2022-07-04 05:57:00 【Small protrusion ~】
Catalog
1. Accept an integer value ( Unsigned ), Print each bit of it in order .
3. seek n The factorial .( Don't think about spillovers )
4. Please n Fibonacci Numbers .( Don't think about spillovers )
5. Write a function reverse_string(char * str)( Recursive implementation )
7. Write a function to implement n Of k Power , Use recursion to implement .
1. Accept an integer value ( Unsigned ), Print each bit of it in order .
for example :
Input :1234, Output 1 2 3 4
Ideas : What we should think about is print Function can type each bit . In this way, there will be the following series of thoughts .
print(1234)
print(123)4
print(12)3 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 functions does not allow the creation of temporary variables , Find the length of the string .
Our function requires string length every time , The parameter passed in is only the first address of the character array , Then we will calculate the length of the first character , 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. seek n The factorial .( Don't think about spillovers )
Ideas :n The factorial = n*(n-1)*(n-2)........*1; And what we define fac Functions are factorial , So as long as you n Pass in the value of .
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. Please n Fibonacci Numbers .( Don't think about spillovers )
Ideas : Fibonacci sequence :1 1 2 3 5 8 13 21 34 55...... And we can use this feature , Write a mathematical expression . 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 )
Realization : Invert the characters in the parameter string , Not in reverse order .
requirement : Out of commission C String manipulation functions in the function library .
such as : "abcdef" become "fedcba"
Ideas : Every time you call reverse_string Function , First, exchange the first and last two characters of the string , But note that the characters at the end need to be filled with characters ‘\0’, Because you want to get the end position of the new string .
#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), 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
Ideas : 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 implement n Of k Power , Use recursion to implement .
Ideas : Be careful k The value of .
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;
}边栏推荐
- Json Web token - jwt vs. Traditional session login Authentication
- Detectron:训练自己的数据集——将自己的数据格式转换成COCO格式
- Arc135 a (time complexity analysis)
- 报错cvc-complex-type.2.4.a: 发现了以元素 ‘base-extension‘ 开头的无效内容。应以 ‘{layoutlib}‘ 之一开头。
- 一键过滤选择百度网盘文件
- XII Golang others
- JS get the attribute values nested in the object
- Impact relay jc-7/11/dc110v
- High performance parallel programming and optimization | lesson 02 homework at home
- C # character similarity comparison general class
猜你喜欢

体验碎周报第 102 期(2022.7.4)

我的NVIDIA开发者之旅——优化显卡性能

High performance parallel programming and optimization | lesson 02 homework at home

Layoutmanager layout manager: flowlayout, borderlayout, GridLayout, gridbaglayout, CardLayout, BoxLayout

How does apscheduler set tasks not to be concurrent (that is, execute the next task after the first one)?

Detailed explanation of common APIs for component and container containers: frame, panel, scrollpane

Online shrimp music will be closed in January next year. Netizens call No

AWT introduction

HMS v1.0 appointment. PHP editid parameter SQL injection vulnerability (cve-2022-25491)

A little understanding of GSLB (global server load balance) technology
随机推荐
Canoe panel learning video
Accidentally deleted the data file of Clickhouse, can it be restored?
[Chongqing Guangdong education] electronic circuit homework question bank of RTVU secondary school
1.1 history of Statistics
Penetration tool - sqlmap
Leetcode question brushing record | 206_ Reverse linked list
2022.7.3-----leetcode.556
buuctf-pwn write-ups (8)
Introduction to AMBA
Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
APScheduler如何设置任务不并发(即第一个任务执行完再执行下一个)?
webrtc 快速搭建 视频通话 视频会议
Configure cross compilation tool chain and environment variables
(4) Canal multi instance use
注释与注解
BUU-Reverse-easyre
Compound nonlinear feedback control (2)
JSON Web Token----JWT和傳統session登錄認證對比
JS arguments parameter usage and explanation
Excel comparator