double
callnext(int
n)
{
if(n>1)
return
callnext(n-1)+3
else
return
1
}
int
main()
{
int
m
scanf("%d",&m)
printf("result=%f",callnext(m))
return
0
}
入口:callnext(m)递归:if(n>1)
return
callnext(n-1)+3中的callnext(n-1);结束:else
return
1整个执行流程:callnext(m)
调用
callnext(m-1)callnext(m-1)调用callnext(m-1-1)。。。
callnext(2)调用callnext(1);callnext(1)=1;结束;
举一个用递归调用函数求输入非负整数的阶乘的例子,如下:
//#include "stdafx.h"//If the vc++6.0, with this line.#include "stdio.h"
int fact(int n){
if(n==1 || n==0) return 1
else return n*fact(n-1)
}
int main(void){
int x
while(1){
printf("Input x(int 12>=x>=0)...\nx=")
if(scanf("%d",&x),x>=0 && x<=12)//x>12时会使结果溢出
break
printf("Error,redo: ")
}
printf("%d! = %d\n",x,fact(x))
return 0
}
阶乘:
要求:给定一个数值,计算出它的阶乘值,例如5的阶乘为5*4*3*2*1
实现:
[html] view plaincopy
<span style="font-size:12px"> // 利用递归实现一个数的阶乘值 private static BigDecimal getNum(BigDecimal inNum) { if (inNum.compareTo(BigDecimal.ONE) == 0) { return inNum } return inNum.multiply(getNum(inNum.subtract(BigDecimal.ONE))) }</span>
(2)Fibonacci数列:1,1,2,3,5,8,13……
要求:找出数列中指定index位置的数值
实现:
[html] view plaincopy
<span style="font-size:12px"> // 利用递归实现了Fibonacci数列 private static int fab(int index) { if (index == 1 || index == 2) { return 1 } else { return fab(index - 1) + fab(index - 2) } }</span>
(3)汉诺塔
要求:汉诺塔挪动
实现:
[html] view plaincopy
<span style="font-size:12px"> <span style="white-space:pre"></span>private static final String DISK_B = "diskB" <span style="white-space:pre"> </span>private static final String DISK_C = "diskC" <span style="white-space:pre"> </span>private static final String DISK_A = "diskA" <span style="white-space:pre"> </span>static String from=DISK_A <span style="white-space:pre"></span> static String to=DISK_C <span style="white-space:pre"></span> static String mid=DISK_B <span style="white-space:pre"></span> public static void main(String[] args) { <span style="white-space:pre"></span> String input=JOptionPane.showInputDialog("please input the number of the disks you want me move.") <span style="white-space:pre"></span> int num=Integer.parseInt(input) <span style="white-space:pre"></span> move(num,from,mid,to) <span style="white-space:pre"></span> }</span>
[html] view plaincopy
<span style="font-size:12px"> // 利用递归实现汉诺塔 private static void move(int num, String from2, String mid2, String to2) { if (num == 1) { System.out.println("move disk 1 from " + from2 + " to " + to2) } else { move(num - 1, from2, to2, mid2) System.out.println("move disk " + num + " from " + from2 + " to " + to2) move(num - 1, mid2, from2, to2) } }</span>
(4)排列组合
要求:将输入的一个字符串中的所有元素进行排序并输出,例如:你给出的参数是"abc",
则程序会输出
abc
acb
bac
bca
cab
cba
实现:
[html] view plaincopy
<span style="font-size:12px"><span style="white-space:pre"> </span>public static void permute(String str) { <span style="white-space:pre"> </span> char[] strArray = str.toCharArray() <span style="white-space:pre"> </span>permute(strArray, 0, strArray.length - 1) <span style="white-space:pre"></span>}</span>
[html] view plaincopy
<span style="font-size:12px"> // 利用递归实现,将输入的一个字符串中的所有元素进行排序并输出 public static void permute(char[] list, int low, int high) { int i if (low == high) { String cout = "" for (i = 0i <= highi++) { cout += list[i] } System.out.println(cout) } else { for (i = lowi <= highi++) { char temp = list[low] list[low] = list[i] list[i] = temp permute(list, low + 1, high) temp = list[low]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)