当前位置:网站首页>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;
}
边栏推荐
- 我的NVIDIA开发者之旅——优化显卡性能
- Win10 clear quick access - leave no trace
- lightroom 导入图片灰色/黑色矩形 多显示器
- 509. 斐波那契数、爬楼梯所有路径、爬楼梯最小花费
- win10清除快速访问-不留下痕迹
- 509. Fibonacci number, all paths of climbing stairs, minimum cost of climbing stairs
- JS arguments parameter usage and explanation
- Risc-v-qemu-virt in FreeRTOS_ Lock mechanism analysis of GCC
- 2022.7.3-----leetcode.556
- 接地继电器DD-1/60
猜你喜欢
Overview of relevant subclasses of beanfactorypostprocessor and beanpostprocessor
如何展开Collapse 的所有折叠面板
input显示当前选择的图片
70000 words of detailed explanation of the whole process of pad openvino [CPU] - from environment configuration to model deployment
Canoe panel learning video
JS扁平化数形结构的数组
JS how to convert seconds into hours, minutes and seconds display
Take you to quickly learn how to use qsort and simulate qsort
注释与注解
JSON web token -- comparison between JWT and traditional session login authentication
随机推荐
How to configure static IP for Kali virtual machine
Halcon image calibration enables subsequent image processing to become the same as the template image
VB. Net GIF (making and disassembling - optimizing code, class library - 5)
Gridview出现滚动条,组件冲突,如何解决
Excel comparator
How to expand all collapse panels
Programmers don't talk about morality, and use multithreading for Heisi's girlfriend
win10清除快速访问-不留下痕迹
卸载Google Drive 硬盘-必须退出程序才能卸载
实用的小工具指令
Steady! Huawei micro certification Huawei cloud computing service practice is stable!
Risc-v-qemu-virt in FreeRTOS_ Lock mechanism analysis of GCC
Impact relay jc-7/11/dc110v
Configure cross compilation tool chain and environment variables
HMS v1.0 appointment. PHP editid parameter SQL injection vulnerability (cve-2022-25491)
注释与注解
left_and_right_net可解释性设计
C语言中的函数(详解)
Design and implementation of tcp/ip series overview
How to determine whether an array contains an element