当前位置:网站首页>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;
}边栏推荐
- C # generates PPK files in putty format (supports passphrase)
- C#生成putty格式的ppk文件(支持passphrase)
- Luogu p1168 median
- Problem solving: how to manage thread_local pointer variables
- Techo youth 2022 academic year college open class: behind the live broadcast of Lianmai, explore how to apply audio and video technology
- [daily record] - bug encountered in BigDecimal division operation
- C#生成putty格式的ppk文件(支持passphrase)
- Oracle临时表详解
- ArrayList分析1-循环、扩容、版本
- ArrayList分析1-循环、扩容、版本
猜你喜欢

Docsify building a personal minimalist knowledge warehouse

20220215 CTF misc buuctf the world in the mirror the use of stegsolve tool data extract

20220215 misc buctf easycap Wireshark tracks TCP flow hidden key (use of WinHex tool)

Redis - how to understand publishing and subscribing

2022-2028 global plant peptone industry research and trend analysis report

Date类的实现

【日常记录】——对BigDecimal除法运算时遇到的Bug

C language file operation for conquering C language

P4 learning - p4runtime

2022-2028 global retro glass industry research and trend analysis report
随机推荐
最长的可整合子数组的长度
What should I do without 50W bride price
Sword finger offer 19 Regular Expression Matching
Ranger plug-in development (Part 2)
MySQL storage engine
连表查询 select 生成
Search rotation sort array
Basic data structure of redis
Date类的实现
2022-2028 global ultra high purity electrolytic iron sheet industry research and trend analysis report
[DaVinci developer topic] -37- detail IRV: introduction to inter runnable variable + configuration
History of deep learning
Bugku CTF daily one question dark cloud invitation code
CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
运动与健康
Can SQL execution be written in tidb dashboard
20220215-ctf-misc-buuctf-einstein-binwalk analyze picture-dd command separate zip file -- look for password in picture attribute
2022-2028 global plant peptone industry research and trend analysis report
Rust controls Dajiang programmable UAV Tello
https://blog.csdn.net/m0_64075307/article/details/122862692?spm=1001.2014.3001.5501