Chinaunix首页 | 论坛 | 博客
  • 博客访问: 583916
  • 博文数量: 92
  • 博客积分: 5026
  • 博客等级: 大校
  • 技术积分: 1321
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-28 11:04
文章分类

全部博文(92)

文章存档

2011年(9)

2010年(17)

2009年(12)

2008年(54)

我的朋友

分类: C/C++

2008-09-25 12:59:05

求 1/1 + 1/2 + 2/3 + 3/5 + 5/8 .... 前 n 项之和!
我太丢人了,弄来弄去还错了一大把!强烈反省,认真学习!
现在总结一下,引以为戒!

CODE:
#include
#include
#include

//递归算法
double func(int n, int a = 1, int b = 1) {
        if(n == 1) {
                return (double)a/b;
        } else {
                return (double)a/b + func(n-1, b, a+b);
        }
}
//非递归算法
double func2(int n) {

        double result = 0;
        int f1,f2;
        f1 = f2 = 1;
        for(int i=0; i                result += (double)f1/f2;
                int temp = f2;
                f2 = f1+f2;
                f1 = temp;
        }
        return result;
}
int main() {

        int a;
        scanf("%d", &a);
        printf("func(%d) = %f\n", a, func(a));
        return 0;


}

从今后我要加强算法了!混不下去了!没脸见人了
阅读(1076) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~