求n!
#include <stdio.h> long fact(int n) { long f; if (n==1) f=1; else f=n*fact(n-1); return f; } int main( ) { int n; long y; scanf("%d", &n); y=fact(n); printf("%ld\n", y); return 0; }
输入一个正整数n,反序输出其各位数
#include <stdio.h> void f(int n) { if (n>0) { printf("%d",n%10); f(n/10); } } int main( ) { int m=1234; f(m); printf("\n"); return 0; }
例:十进制转二进制的递归算法
#include <stdio.h> void f(int n) { if (n>0) { f(n/2); printf("%d",n%2); } } int main( ) { int m=1234; f(m); printf("\n"); return 0; }
例:二分查找的递归表示和实现
#include <stdio.h> #define SIZE 12 int r_search(int arr[], int low, int high, int k); int main( ) { int d[SIZE] = { 1, 3, 9, 12, 32, 41, 45, 62, 75, 77, 82, 95}; int key,index; printf("输入要查找的关键字:\n"); scanf("%d", &key); index = r_search(d, 0, SIZE-1, key); if(index >= 0) printf("关键字所在位置是:%d \n", index); else printf("没有找到关键字\n"); return 0; } int r_search(int arr[], int low, int high, int k) { int i,mid; if (low>high) i=-1; else { mid=(low+high)/2; if(arr[mid]==k) i=mid; else if(arr[mid]>k) i=r_search(arr, low,mid-1,k); else i=r_search(arr, mid+1,high,k); } return i; }