飞网论坛

标题: 173、递归算法题2 [打印本页]

作者: johnny    时间: 2016-9-7 23:17
标题: 173、递归算法题2

173、递归算法题2


第1个人10,第2个比第1个人大2岁,依次递推,请用递归方式计算出第8个人多大?

package cn.itcast;


import java.util.Date;


public class A1 {


public static void main(String [] args)

{

System.out.println(computeAge(8));

}

public static int computeAge(int n)

{

if(n==1) return 10;

return computeAge(n-1) + 2;

}

}

public static void toBinary(int n,StringBuffer result)

{


if(n/2 != 0)

toBinary(n/2,result);

result.append(n%2);

}







欢迎光临 飞网论坛 (https://bbs.cfei.net/) Powered by Discuz! X3.2