//函数的递归
//例1.接收一个整型值,并且按顺序打印他的每一位.(例如:输入:1234.输出:1 2 3 4)
#include<stdio.h>
#include <stdlib.h>
void print(int num){
if (num > 9){
print(num / 10);
}
printf("%d\n", num % 10);
}
int main(){
print(1234);
system("pause");
return 0;
}
operation result:
//例2.求字符串的长度
//法1.利用strlen函数直接求
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
int main(){
char arr[] = "abcd";
int ret;
ret = strlen(arr);
printf("%d\n",ret);
system("pause");
return 0;
}
//法2.利用函数的形式进行求解
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
//"abcd"
int Strlen(char arr[]){
int i = 0;
while (arr[i] != '\0' ){
i++;
}
return i;
}
int main(){
char arr[] = "abcd";
printf("%d\n", Strlen(arr));
system("pause");
return 0;
}
//法3.递归
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//"abcd"
int Strlen(char arr[]){
int i = 0;
if (arr[i] == '\0'){
return 0;
}
return 1 + Strlen(arr+1);//指针+1等于跳过一个元素指向下一个字符
}
int main(){
char arr[] = "abcd";
printf("%d\n", Strlen(arr));
system("pause");
return 0;
}
Run Results:
Summary:
About Solution summary string: Method 1 is directly strlen function Solution Method 2 is to use as a function of the introduction of parameters is calculated.
Method 3 using recursive method call solved must be understood recursively. the way
the string is a special array of characters encountered \ 0 on the end must end with \ 0. calculate its length. length is not \ 0
character array length :( account the number of bytes of memory) must be calculated length count \ 0.
//例3.求n的阶乘
//法1.直接法
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <stdlib.h>
int main(){
int i;
int n;
int ret=1;
printf("请输入要求解的阶乘: \n");
scanf("%d",&n);
for (i = 1; i <= n; i++){
ret = ret*i;
}
printf("%d \n",ret);
system("pause");
return 0;
}
//法2.函数的调用
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void Factor(int N){
int ret = 1;
int i;
for (i = 1; i <= N; i++){
ret = ret*i;
}
printf("%d\n",ret);
}
int main(){
int n;
printf("请输入要求解的阶乘: \n");
scanf("%d",&n);
Factor(n);
system("pause");
return 0;
}
Use function calls, when the end back to the next line started calling location
//法3.递归实现
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Factor(int N){
if (N == 1){
return 1;
}
return N*Factor(N - 1);
}
int main(){
int n;
printf("请输入要求解的阶乘: \n");
scanf("%d",&n);
printf("%d\n", Factor(n));
system("pause");
return 0;
}
Recursive function is to split the problem, then recursively solved.
The result:
//例4.斐波那契数(生兔子)
1 1 2 3 5 8 13 21 .........()始终满足前两项之和等于第三项
法1.递归(函数调用)计算方法比递归快很多避免了重复计算
#include <stdio.h>
#include <stdlib.h>
int Fib(int n){
int last1 = 1;
int last2 = 1;
int cur;
int i;
if (n <= 2){
return 1;;
}
if (n >= 3){
for (i = 3; i <= n; i++){
cur = last1 + last2;
last2 = last1;
last1 = cur;
}
return cur;
}
}
int main(){
printf ("%d\n",Fib(1));
system("pause");
return 0;
}
//法2.递归
#include <stdio.h>
#include <stdlib.h>
int Fib(int n){
if (n <= 2){
return 1;
}
return Fib(n - 1) + Fib(n - 2);
}
int main(){
printf("%d\n",Fib(6));
system("pause");
return 0;
}
The result:
a recursive Summary:
recursive function is to split the problem, then solve recursively.
Recursive: programming skills program that calls itself recursively two necessary conditions.
1. restrictions exist when this limit when conditions are met, recursion will no longer be continued.
2. after each recursive call closer and closer to this restriction. (convergence)