1.递归和非递归分别实现求第n个斐波那契数。
递归函数实现:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int Fibonacci(int n) {
if ( n < 3 ) {
return n;
}
return Fibonacci(n - 2) + Fibonacci(n - 1);
}
int main(void){
int arr[100] = { 1, 1 };
int n = 0;
printf("请输入你想得到的斐波那契数列长度:\n");
scanf("%d", &n);
for (int i = 2; i < n; i++) {
arr[i] = Fibonacci(i);
}
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
system("pause");
return 0;
}
非递归实现:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main(void){
int arr[100] = { 1, 1 };
int n = 0;
printf("请输入你想得到的斐波那契数列长度:\n");
scanf("%d", &n);
for (int i = 2; i < n; i++) {
arr[i] = arr[i - 2] + arr[i - 1];
}
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
system("pause");
return 0;
}
2.编写一个函数实现n^k,使用递归实现
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int TimesSquare(int num , int k) {
if (k == 0) {
return 1;
}
return TimesSquare(num , k - 1) * num;
}
int main(void){
int num = 0;
int times = 0;
printf("请输入底数n = ");
scanf("%d", &num);
printf("请输入次方数times = ");
scanf("%d", ×);
printf("%d \n", TimesSquare(num, times));
system("pause");
return 0;
}
3. 写一个递归函数DigitSum(n),输入一个非负整数,返回组成它的数字之和,
例如,调用DigitSum(1729),则应该返回1+7+2+9,它的和是19
#include <stdio.h>
#include <stdlib.h>
int DigitSum(int num) {
static int sum = 0;
if (num > 9) {
DigitSum(num / 10);
}
return sum = sum + num % 10;
}
int main(void){
printf("%d ", DigitSum(1729));
system("pause");
return 0;
}
4. 编写一个函数 reverse_string(char * string)(递归实现)
实现:将参数字符串中的字符反向排列。
要求:不能使用C函数库中的字符串操作函数。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void reverseString(char* str) {
int i = strlen(str) - 1;
char temp = '0';
temp = str[0];
str[0] = str[i];
str[i] = '\0';
reverseString(str + 1);
str[i] = temp;
}
int main(void) {
char* string = "abcd";
reverseString(string);
printf("\n");
for (; *string != '\0'; string++) {
printf("%c", *string);
}
system("pause");
return 0;
}
5.递归和非递归分别实现strlen
递归实现:
#include <stdio.h>
#include <stdlib.h>
int StrLen(char* string) {
static int count = 0;
if (*string != '\0') {
StrLen(string + 1);
count++;
}
return count;
}
int main(void){
printf("%d \n", StrLen("abcd"));
system("pause");
return 0;
}
非递归实现
#include <stdio.h>
#include <stdlib.h>
//非递归函数实现strlen
int StrLenT(char* string) {
static int count = 0;
while (*string != '\0') {
count++;
string++;
}
return count;
}
int main(void) {
printf("%d \n", StrLenT("abcdffff"));
system("pause");
return 0;
}
6.递归和非递归分别实现求n的阶乘
递归函数实现:
#include <stdio.h>
#include <stdlib.h>
int factorial(int num) {
if (num == 1) {
return 1;
}
return factorial(num - 1) * num;
}
int main(void){
printf("%d \n", factorial(10));
system("pause");
return 0;
}
非递归实现:
#include <stdio.h>
#include <stdlib.h>
int factorial_T(int num) {
int product = 1;
while (num != 0) {
product = product * num;
num--;
}
return product;
}
int main(void) {
printf("%d \n", factorial_T(10));
system("pause");
return 0;
}
7.递归方式实现打印一个整数的每一位
#include <stdio.h>
#include <stdlib.h>
void printfAll(int num) {
if (num > 9) {
printfAll(num / 10);
}
printf("%d " , num % 10);
}
int main(void){
printfAll(1234);
system("pause");
return 0;
}