写一递回程式求以下数列之和(n可任意输入)

[复制链接]
查看11 | 回复3 | 2013-1-8 11:50:03 | 显示全部楼层 |阅读模式
对于每一项(n>1),可以表示为f(n) = 2(1/n-1/(n+1))
float sum = 0.0f;
public float sump(int n){
if(n<2) sum+= 1f;
else{
float temp =2*(1f/n-1f/(n+1));
sum += temp+sump(n-1);
}
return sum;
}...
回复

使用道具 举报

千问 | 2013-1-8 11:50:03 | 显示全部楼层
public class test { public static void main(String[] args) {test t = new test();System.out.println(t.Count(5)); }//递归累加 public double Count(int n){if(1==n)...
回复

使用道具 举报

千问 | 2013-1-8 11:50:03 | 显示全部楼层
for循环就可以实现,不知道你的递回什么意思...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行