当前位置:网站首页>Exercises on recursion in C language
Exercises on recursion in C language
2022-07-01 00:58:00 【Sky • Shang】
of Recursive knowledge points stay :
subject 1:
Write a function reverse_string(char * string)( Recursive implementation )
Realization : Invert the characters in the parameter string , Not in reverse order .
requirement : No Be able to use C String manipulation functions in the function library .
such as :
char arr[] = "abcdef"; In reverse order, the contents of the array become :fedcba
Code implementation :( Non recursive methods )
#include <stdio.h>
#include <string.h>
int main()
{
char arr[] = "abcdef";
int sz = strlen(arr); // Use strlen Function requires the introduction of header files
char *right = arr + (sz - 1); // Because of the particularity of array subscripts , So you have to subtract 1
char *left = arr; // Note that the type of pointer is char, instead of int
while (left < right)
{
char change = *left;
*left = *right;
*right = change;
left++; // If the pointer wants to ++, You need to create a pointer variable for it first
right--;
}
printf("%s\n", arr);
return 0;
}Be careful :
Use strlen Function requires the introduction of header files
char *right = arr + (sz - 1); // Because of the particularity of array subscripts , So you have to subtract 1
char *left = arr; // Note that the type of pointer is char, instead of int
left++; // If the pointer wants to ++ or --, You need to create a pointer variable for it first
Code implementation ( Recursive method )
#include <stdio.h>
#include <string.h>
void exchange(char *str)
{
int sz = strlen(str); // Notice that formal parameters are used in parentheses , Not arguments
char change = *str;
*str = *(str + sz - 1);// The order of the two sides of the equal sign cannot be reversed
*(str + sz - 1) = '\0';
if (strlen(str + 1) >= 2)// At this point, the condition of recursion is the number of strings
{
exchange(str + 1);
}
*(str + sz - 1) = change; // Because exchange requires two steps
}
int main()
{
char arr[] = "abcdef";
exchange(arr);
printf("%s\n", arr);
return 0;
} int sz = strlen(str);// Notice that formal parameters are used in parentheses , Not arguments
*str = *(str + sz - 1);// The order of the two sides of the equal sign cannot be reversed
if (strlen(str + 1) >= 2)// At this point, the condition of recursion is the number of strings
*(str + sz - 1) = change; // Because exchange requires two steps
subject 2:
Calculate the sum of each of a number ,
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
Code implementation :
#include <stdio.h>
int digitsum(unsigned int scan)
{
int first = scan % 10;
if (scan > 9) // Note that the condition here is the number of digits
{
return first + digitsum(scan / 10);
}
else
{
return scan;
}
}
int main()
{
unsigned int data = 0;
scanf_s("%d", &data);//scanf_s Function %d It's best not to add line breaks
int sum = digitsum(data);
printf("%d\n", sum);
return 0;
}subject 3:
Write a function to implement n Of k Power , Use recursion to implement
#include <stdio.h>
double calculation(int base,int n) // Note that double type
{
if (n > 0)
{
return base * calculation(base, n - 1);
}
else if (n == 0)
{
return 1;
}
else if (n < 0) // Don't forget that the exponent can be negative
{
return 1.0 / calculation(base, -n);
}
}
int main()
{
int base = 0;
int n = 0; //n Is an integer
scanf_s("%d %d", &base, &n);
double sum = calculation(base, n); // The result must be of decimal type
printf("%lf\n", sum);
return 0;
}边栏推荐
- Pytorch auto derivation
- Line number of Jenkins pipeline script execution exception
- leetcode 474. Ones and zeroes (medium)
- Solving the weird problem that the query conditions affect the value of query fields in MySQL query
- 写给 5000 粉丝的一封信!
- Some views on libco
- leetcode 474. Ones and Zeroes 一和零(中等)
- Oracle temporary table explanation
- HDU 2488 A Knight's Journey(DFS)
- Oracle临时表详解
猜你喜欢

初识 Flutter 的绘图组件 — CustomPaint

Oracle temporary table explanation

Line number of Jenkins pipeline script execution exception

C language file operation for conquering C language

Oracle临时表详解

Oracle data integrity

Set different background colors for the border and text of the button

剑指 Offer 18. 删除链表的节点

20220215 CTF misc buuctf Xiaoming's safe binwalk analysis DD command separate rar file archpr brute force password cracking

2022-2028 global ICT test probe industry research and trend analysis report
随机推荐
实验八 T-sql,存储过程
Excuse me, does Flink support synchronizing data to sqlserver
How to specify the number of cycles in JSTL- How to loop over something a specified number of times in JSTL?
A letter to 5000 fans!
连表查询 select 生成
The quantity and quality of the devil's cold rice 101; Employee management; College entrance examination voluntary filling; Game architecture design
Interface documentation system - Yapi
【日常记录】——对BigDecimal除法运算时遇到的Bug
给按钮的边框和文字设置不同的背景色
【2023联发科提前批笔试题】~ 题目及参考答案
2022-2028 global ethylene oxide scrubber industry research and trend analysis report
Pytorch auto derivation
运动与健康
P4学习——p4runtime
CentOS install MySQL
第53章 从业务逻辑实现角度整体性理解程序
Some views on libco
MySQL storage engine
Line number of Jenkins pipeline script execution exception
20220215-ctf-misc-buuctf-einstein-binwalk analyze picture-dd command separate zip file -- look for password in picture attribute
https://blog.csdn.net/m0_64075307/article/details/122862692?spm=1001.2014.3001.5501