用java递归算法,求1+2+4+8+~~~的和?
发布网友
发布时间:2024-09-30 18:01
我来回答
共2个回答
热心网友
时间:2024-10-30 00:20
即f(x)=f(x-1)*2;f(1)=1;......
public class Test4 {
public static void main(String[]args){
System.out.println(num(10));
}
public static int f(int x){
if(x == 1)return 1;
return f(x - 1)*2;
}
public static int num(int x){
if(x == 1)return f(1);
return num(x - 1)+f(x);
}
}
热心网友
时间:2024-10-30 00:20
int count(int n)
{
if(n==1)
return 1;
else
return count(n-1)+n;
}