笔试题:第1个人10,第2个比第1个人大2岁,依次递推,请用递归方式计算出第8个人多大?
下面是我的实现方法
class Algorithm {
private static Algorithm instance;
private Algorithm(){}
public int computeAge(int n) throws IllegalAccessException {
if(n < 1)
throw new IllegalAccessException();
if(n == 1) {
return 10;
}
return computeAge(n -1) + 2;
}
public static synchronized Algorithm getInstance() {
if(instance == null) {
instance = new Algorithm();
}
return instance;
}
}
public class Main {
public static void main(String[] args) throws IllegalAccessException {
Algorithm algorithm = Algorithm.getInstance();
System.out.println(algorithm.computeAge(100));
}
}