6-8 斐波那契数列(六)

来源

教材【练习7-18】。

问题描述

(1) 编写一个C语言函数,函数名字是print_fib,返回值是int类型,参数列表有2个long long类型变量m和n作为形式参数。

函数print_fib( )的功能是根据给定的m值和n值计算并输出斐波那契数列中数值介于m与n之间(包含m与n)的项,项与项之间使用空格分隔,行末没有空格和换行符。同时统计所输出项的数量并作为返回值返回。如果没有输出任何项,则返回值为0。函数print_fib( )不允许从键盘读取数据。

函数print_fib( )对应的函数原型如下:

int print_fib(long long m, long long n);

裁判测试程序样例:

#include <stdio.h>
int print_fib(long long m, long long n);
int main()
{
    long long m, n;
    int counter;
    scanf("%lld %lld", &m, &n);
    if((m >= 0) && (n > m)){
        counter = print_fib(m, n);
        printf("\n%d", counter);
    }else{
        printf("error");
    }
    return 0;
}
// 你编写的代码将会嵌入到这里

输入样例:

123456789012  1234567890123

输出样例:

139583862445 225851433717 365435296162 591286729879 956722026041
5

代码长度限制		16 KB
时间限制			400 ms
内存限制			64 MB

参考代码:

int print_fib(long long m, long long n)
{
    long long x=0;
    long long y=1;
    long long z=x+y;
    int c=0;
    if(m==0){
        printf("%lld %lld ",x,y);
        c+=2;
    }else if(m==1){
        printf("%lld ",y);
        c+=1;
    }
    while(z<=n){
        if(z>=m){
            if(y+z<=n){
                printf("%lld ",z);
                c++;
            }else{
                printf("%lld",z);
                c++;
            }
        }
        x=y;
        y=z;
        z=x+y;
    }
    return c;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值