1.递归和非递归分别实现求第n个斐波那契数。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
//用递归实现求第几个斐波那契数
int fabonacci(int n)
{
if (n <= 2)
return 1;
else
return fabonacci(n - 1) + fabonacci(n - 2);
}
int main()
{
int n = 3;
printf("请输入要求第几个斐波那契数:");
scanf("%d", &n);
int ret=fabonacci(n);
printf("ret=%d\n", ret);
system("pause");
return 0;
}
```java``
//用非递归实现求第几个斐波那契数(迭代)
int fabonacci(int n)
{
int a = 1;
int b = 1;
int c = 1;
while (n > 2)
{
c = a + b;
a = b;
b = c;
n–;
}
return c;
}
int main()
{
int n=0;
printf(“请输入要求第几个斐波那契数:”);
scanf("%d", &n);
int ret = fabonacci(n);
printf(“ret=%d\n”, ret);
system(“pause”);
return 0;
}
2.编写一个函数实现n^k,使用递归实现。
```java
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
int fun(int a, int b)
{
if (b == 0) //判断指数是否为零
{
return 1;
}
else if (b == 1)
{
return a;
}
else
{
return a * fun(a, b - 1);
}
}
int main()
{
int n = 0;
int k = 0;
printf("n^k,请输入n,k:\n");
scanf("%d %d", &n,&k);
printf("运算结果:\n");
printf("%d^%d=%d\n",n,k,fun(n,k));
system("pause");
return 0;
}
3.写一个递归函数DigitSum(n),输入一个非负整数,返回组成它的数字之和。例如:调用DigitSum(1729),则应该返回1+7+2+9,它的和是19。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int DigitSum(int n)
{
if (n == 0)
return 0;
else
{
return (n % 10 + DigitSum(n / 10));
}
}
int main()
{
int n;
printf("请输入一个非负整数:\n");
scanf("%d", &n);
int ret = DigitSum(n);
printf("%d\n", ret);
system("pause");
return 0;
}
4.编写一个函数reverse_string(char*string) (递归实现)
实现:将参数字符串中的字符反向排列。
要求:不能使用C函数库中的字符串操作函数。
/*
编写一个函数reverse_string(char*string) (递归实现)
实现:将参数字符串中的字符反向排列。
要求:不能使用C函数库中的字符串操作函数。
*/
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
void reverse_string(char * string)
{
if (*(++string) != '\0')
{
reverse_string(string);
}
printf("%c", *(string - 1));
}
int main()
{
char*p = "abcdef";
reverse_string(p);
printf("\n");
system("pause");
return 0;
}
5.递归和非递归分别实现strlen。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//递归实现
int Strlen(char* str)
{
if (*str == '\0')
{
return 0;
}
else
{
return Strlen(str + 1) + 1;
}
}
int main()
{
char str[30] = {0};
printf("请输入一个字符串:\n");
gets(str);
printf("字符串长度是:\n");
printf("%d\n", Strlen(str));
system("pause");
return 0;
}
//非递归
int Strlen(char* str)
{
int n = 0;
while (*str != '\0')
{
++n;
++str;
}
return n;
}
int main()
{
char str[30] = {0};
printf("请输入一个字符串:\n");
gets(str);
printf("字符串长度是:%d\n", Strlen(str));
system("pause");
return 0;
}
6.递归和非递归分别实现求n的阶乘。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
//递归
int factorial(int n)
{
if(n==1)
{
return 1;
}
else
{
return n * factorial(n - 1);
}
}
int main()
{
int n = 0;
printf("请输入一个数字:\n");
scanf("%d", &n);
printf("该数的阶乘为%d\n", factorial(n));
system("pause");
return 0;
}
//非递归
int factorial(int n)
{
int num = 1;
for (int i = 1; i <= n; i++)
{
num *= i;
}
return num;
}
int main()
{
int n = 0;
printf("请输入一个数字:\n");
scanf("%d", &n);
printf("该数的阶乘为%d\n", factorial(n));
system("pause");
return 0;
}
7.递归方式实现打印一个整数的每一位。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int print(int n)
{
if (n > 9)
{
print ( n /10);
}
printf("%2d", n % 10);
}
int main()
{
int n;
printf("请输入一个整数:\n");
scanf("%d", &n);
print(n);
system("pause");
return 0;
}