| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 593 人关注过本帖
标题:一个Fibonacci算法,不知道到底是哪里出错了,要怎么改啊
只看楼主 加入收藏
bid2938692
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2009-9-23
结帖率:76.92%
收藏
已结贴  问题点数:6 回复次数:2 
一个Fibonacci算法,不知道到底是哪里出错了,要怎么改啊
#include "stdio.h"
#define true 1
#define false 0
typedef int keytype;
typedef int datatype;
typedef struct{   
    keytype key;
    datatype other;
}rectype;
int fibonacci(n)
int n;
{    if(n==0)
        return(0);
    else
        if(n==1)
            return(1);
        else
            return (fibonacci(n-1)+fibonacci(n-2));
}
int fibosrch(rectype R[],keytype k,int n)
    {    int i,p,q,s,t,flag1,flag2;
        i=fibonacci(n-1);
        p=fibonacci(n-2);
        q=fibonacci(n-3);
        s=n+1-(i+p);
        if(k>R[i].key)
            i=i+s;
        flag1=false;
        while((i)&&(!flag1))
        {    if(R[i].key==k)
                flag2=1;
            else
                if(R[i].key<k)
                    flag2=2;
                else
                    if(R[i].key>k)
                        flag2=3;
            switch(flag2)
            {case 1:flag1=true;
                    break;
             case 2:if(q==0)
                    i=0;
                    else
                    {i=i-q;
                     t=p;
                     p=q;
                     q=t-q;
                    }
                    break;
             case 3:if(p==1)
                        i=0;
                    else
                    {    i=i+q;
                        p=p-q;
                        q=q-p;
                    }
                    break;
             default;
            }
        }
        return (i);
    }
int main()
{    int a,i,k;
    rectype R[15];
    scanf("%d",&k);
    for(i=1;i<=15;i++)
        scanf("%d",R[i].key);
    a=fibosrch(R,k,15);
    printf("%d",a);
    return 0;
}
代码是按照课本原样输入的,不知道要怎么改了~~~~到底是哪里出错呢
搜索更多相关主题的帖子: Fibonacci 算法 
2010-05-31 23:36
lijm1989
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:珠海
等 级:贵宾
威 望:12
帖 子:675
专家分:2844
注 册:2009-10-14
收藏
得分:6 
#include "stdio.h"
#define true 1
#define false 0
typedef int keytype;
typedef int datatype;
typedef struct{   
    keytype key;
    datatype other;
}rectype;
int fibonacci(int n)
//  int n;

{    if(n==0)
        return(0);
    else
        if(n==1)
            return(1);
        else
            return (fibonacci(n-1)+fibonacci(n-2));
}
int fibosrch(rectype R[],keytype k,int n)
    {    int i,p,q,s,t,flag1,flag2;
        i=fibonacci(n-1);
        p=fibonacci(n-2);
        q=fibonacci(n-3);
        s=n+1-(i+p);
        if(k>R[i].key)
            i=i+s;
        flag1=false;
        while((i)&&(!flag1))
        {    if(R[i].key==k)
        flag2=1;
        else
            if(R[i].key<k)
                flag2=2;
            else
                if(R[i].key>k)
                    flag2=3;
                switch(flag2)
                {case 1:flag1=true;
                break;
                case 2:if(q==0)
                           i=0;
                    else
                    {i=i-q;
                    t=p;
                    p=q;
                    q=t-q;
                    }
                    break;
                case 3:if(p==1)
                           i=0;
                    else
                    {    i=i+q;
                    p=p-q;
                    q=q-p;
                    }
                    break;
                default: ;
                }
        }
        return (i);
}
int main()
{    int a,i,k;
rectype R[15];
scanf("%d",&k);
for(i=1;i<=15;i++)
scanf("%d",&R[i].key);
a=fibosrch(R,k,15);
printf("%d",a);
return 0;
}
这样??
2010-06-01 19:58
bid2938692
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2009-9-23
收藏
得分:0 
楼上的那个是什么编译器捏??
2010-06-01 21:34
快速回复:一个Fibonacci算法,不知道到底是哪里出错了,要怎么改啊
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.050031 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved