当前位置:网站首页>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;
}边栏推荐
- 2022-2028 global elevator emergency communication system industry research and trend analysis report
- [daily record] - bug encountered in BigDecimal division operation
- Get to know the drawing component of flutter - custompaint
- Using Excel to quickly generate SQL statements
- 2022-2028 global rampant travel industry research and trend analysis report
- Interface documentation system - Yapi
- 运动与健康
- Host FL Studio fruit music production daw20.9
- C#生成putty格式的ppk文件(支持passphrase)
- On the application of cluster analysis in work
猜你喜欢

Confirm() method of window

HDU 2488 A Knight's Journey(DFS)

2022-2028 global capsule shell industry research and trend analysis report

剑指 Offer 19. 正则表达式匹配

Host FL Studio fruit music production daw20.9

2022-2028 global electric yacht industry research and trend analysis report

Redis - understand the master-slave replication mechanism

Oracle-表的创建与管理

C language file operation for conquering C language

2022-2028 global 3D printing ASA consumables industry research and trend analysis report
随机推荐
CentOS installation starts redis
2022就要过去一半了,挣钱好难
P4 learning - Basic tunneling
The quantity and quality of the devil's cold rice 101; Employee management; College entrance examination voluntary filling; Game architecture design
CentOS install MySQL
优质的水泵 SolidWorks模型素材推荐,不容错过
Length of the longest integrable subarray
对libco的一点看法
Examples of topological sequences
Some views on libco
Join table query select generation
Bugku CTF daily one question dark cloud invitation code
2022-2028 global 3D printing ASA consumables industry research and trend analysis report
Implementation of date class
Yboj mesh sequence [Lagrange interpolation]
2022-2028 global carbon fiber room scraper system industry research and trend analysis report
Interface documentation system - Yapi
Sword finger offer 18 Delete the node of the linked list
Teach you how to use Hal library to get started -- become a lighting master
2022-2028 global electric yacht industry research and trend analysis report
https://blog.csdn.net/m0_64075307/article/details/122862692?spm=1001.2014.3001.5501