| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 651 人关注过本帖
标题:一个关于Fibonacci的算法,不知道出错在哪里
只看楼主 加入收藏
bid2938692
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2009-9-23
结帖率:76.92%
收藏
 问题点数:0 回复次数: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:38
飘到心海
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:79
专家分:145
注 册:2009-5-3
收藏
得分:0 
程序代码:
#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)
{    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:51
飘到心海
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:79
专家分:145
注 册:2009-5-3
收藏
得分:0 
刚才没把改正的地方突出出来,再贴一个,编译连接没问题了,就是不知道运行怎么样。
#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)
{    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:52
快速回复:一个关于Fibonacci的算法,不知道出错在哪里
数据加载中...
 
   



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

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