当前位置:网站首页>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;
}
边栏推荐
- MySQL的information_schema数据库
- Weekly summary (*63): about positive energy
- AWT common components, FileDialog file selection box
- BUU-Crypto-Cipher
- One click filtering to select Baidu online disk files
- 冲击继电器JC-7/11/DC110V
- js如何将秒转换成时分秒显示
- VB. Net GIF (making and disassembling - optimizing code, class library - 5)
- Configure cross compilation tool chain and environment variables
- Basic concept of bus
猜你喜欢
BUU-Reverse-easyre
Detailed explanation of common APIs for component and container containers: frame, panel, scrollpane
Introduction to AMBA
报错cvc-complex-type.2.4.a: 发现了以元素 ‘base-extension‘ 开头的无效内容。应以 ‘{layoutlib}‘ 之一开头。
(4) Canal multi instance use
Review | categories and mechanisms of action of covid-19 neutralizing antibodies and small molecule drugs
BUU-Crypto-Cipher
实用的小工具指令
js如何将秒转换成时分秒显示
buuctf-pwn write-ups (8)
随机推荐
Programmers don't talk about morality, and use multithreading for Heisi's girlfriend
BUU-Real-[PHP]XXE
Take you to quickly learn how to use qsort and simulate qsort
测试岗的中年危机该如何选择?是坚守还是另寻出路?且看下文
Win10 clear quick access - leave no trace
Online shrimp music will be closed in January next year. Netizens call No
A little understanding of GSLB (global server load balance) technology
LC weekly 300
BUU-Crypto-Cipher
ES6 modularization
ANSYS command
Nexus 6p从8.0降级6.0+root
AWT introduction
Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
High performance parallel programming and optimization | lesson 02 homework at home
Nexus 6p downgraded from 8.0 to 6.0+root
My NVIDIA developer journey - optimizing graphics card performance
js获取对象中嵌套的属性值
Qt发布多语言国际化翻译
BeanFactoryPostProcessor 与 BeanPostProcessor 相关子类概述