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语言 输出斐波那契数列前n个数
- (C语言)用'*'输出指定行数的矩形
- 用递归和非递归的方法输出斐波那契数列的第n个元素(C语言实现)
- C语言中格式字符指定输出项的数据类型和输出格式总结
- 输出指定长度的斐波那契数列
- C语言printf——动态指定输出格式长度
- 输出指定区间的九九乘法表
- 4-21 使用函数输出指定范围内的Fibonacci数 (10分)
- C语言宏定义实现闰年判断并输出指定月的天数
- C语言输出数据到指定文件,要求长度相同,不同时前面补零
- C语言printf——动态指定输出格式长度
- [新手编程训练项目]005——C语言文件夹文件信息输出002:递归输出指定文件夹中的文件信息
- 【Java学习笔记】接收命令行参数输出指定个数的斐波那契数列
- 【C语言小练习】输入十个数,从其中删除指定数字(可能多个)后,输出剩余的数(顺序与输入的相同)
- c语言printf--动态指定输出格式长度
- C语言递归方法实现斐波那契数列
- c语言获取shell脚本输出 -- 管道方式
- C语言输出“I LOVE U”
- logback 日志不输出到指定文件
- <C语言>printf的对齐输出