您的位置:首页 > 编程语言 > C语言/C++

C语言 - 斐波那契数列(Fibonacci) 指定区间输出

2013-12-07 15:07 330 查看
题目:
题目:
输入一个正整数 repeat(0<repeat<10), 做repeat 次下列运算:
输入 2 个正整数 m 和 n (1<=m, n<=10000), 输出 m 和 n 之间所有的Fibonacci数。Fibonacci序列(第 1 项起):1 1 2 3 5 8 13 21 .....
// 这个是我精简后的代码,下面有一个雏形,精简代码很有趣的
// 大家可以把你以前的代码全部翻出来精简一下吧
#include <stdio.h>
int main()
{
    int repeat, m, n, f1, f2, f3;
    scanf("%d", &repeat);
    while(repeat-- && scanf("%d %d", &m, &n))
    {
        f1 = f2 = 1;
        while(f1 <= n)
        {
            if(f1 >= m)
                printf("%d ", f1);
            f3 = f1 + f2;    f1 = f2;    f2 = f3;    
        }puts("");
    }
    return 0;
}

// 再一次精简代码!
#include <stdio.h>
int main()
{
    int repeat, q = 1, m, n, f1, f2, f3;
    while( (q ? scanf("%d", &repeat) : repeat--), q = 0, repeat && scanf("%d %d", &m, &n) )
// while( (q ? scanf("%d", &repeat),q = 0 : q ),repeat-- && ~scanf("%d %d", &m, &n) )
     {
        f1 = f2 = 1;
        while(f1 <= n)
        {
            if(f1 >= m)
                printf("%d ", f1);
            f3 = f1 + f2;    f1 = f2;    f2 = f3;    
        }puts("");
    }
    return 0;
}
// 脑残的时候写的,可以说是很不好的吧
#include <stdio.h>
#include <stdlib.h>

void main(void)
{
int repeat, i, j, m, n, f1, f2, f3;

scanf("%d", &repeat);

for(i=1; i<=repeat; i++)
{
scanf("%d %d", &m, &n);

f1 = f2 = 1;

if(m == 1)
{
printf("%d %d ", f1, f2);
}

f3 = f1 + f2;

while(f3 <= n)
{
if(f3 == 2 && m == 1)
{
printf("%d ", f3);
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
else if(f2 >= m)
{
printf("%d ", f3);
}
f3 = f1 + f2;
f1 = f2;
f2 = f3;

}
printf("\n");
}

printf("\n");
system("pause");
return 0;
}
/*
--------------在VC++中显示--------------
3
1 10
1 1 2 3 5 8
20 100
21 34 55 89
1000 6000
1597 2584 4181
*/
// 计算机科学技术(354867750)
// 这个值得学习
#include<stdio.h>
int main()
{
int x, y, n,F,a,i,z, a1[81],a2[81];
scanf("%d",&a);
for (i=0;i<a;i++)
scanf("%d %d",&a1[i],&a2[i]);
for (z=0;z<a;z++)
{
x = y = 1;	// 每次进入都初始化这个x, y
for (n=0;n<35;n++)
{
if (x>=a1[z] && x<=a2[z])
printf("%d ", x);
F=x+y;
x=y;
y=F;
}
puts("");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C语言 王吉平