zcmu 1095
1095: 输出 Fibonacci 序列
Description
输入一个正整数 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 .....
Input
见sample
Output
见sample
Sample Input
3
1 10
20 100
1000 6000
Sample Output
1 1 2 3 5 8
21 34 55 89
1597 2584 4181
思路:将小于n(包括n在内)的斐波拉契数存入数组中,遍历数组,直到数值大于等于m便开始输出,直到输出的数小于等于n。注意输出格式。
代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<time.h>
using namespace std;
#define FORA(i,x,y) for(int i = x; i < y; i++)
#define FORB(i,x,y) for(int i = x; i <= y; i++)
#define FORC(i,y,x) for(int i = y; i > x; i--)
#define maxn 100000
#define INF 1000000000
#define LL long long
const int mod = 1000000;
int a[maxn];
int b[maxn];
void fbn(int m,int *a){
a[1] = 1;
a[2] = 1;
for(int i = 3; ;i++){
a[i] = a[i-1] + a[i-2];
if(a[i] > m) break;
}
}
int main(){
int t,n,m;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m);
fbn(m,a);
int k = 0;
for(int i = 1; ;i++){
if(a[i] >= n && a[i] <= m) {
b[k++] = a[i];
}
if(a[i] > m) break;
}
printf("%d",b[0]);
FORA(i,1,k) {
printf(" %d",b[i]);
}
printf("\n");
}
return 0;
}
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<time.h>
using namespace std;
#define FORA(i,x,y) for(int i = x; i < y; i++)
#define FORB(i,x,y) for(int i = x; i <= y; i++)
#define FORC(i,y,x) for(int i = y; i > x; i--)
#define maxn 100000
#define INF 1000000000
#define LL long long
const int mod = 1000000;
int a[maxn];
int b[maxn];
void fbn(int m,int *a){
a[1] = 1;
a[2] = 1;
for(int i = 3; ;i++){
a[i] = a[i-1] + a[i-2];
if(a[i] > m) break;
}
}
int main(){
int t,n,m;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m);
fbn(m,a);
int k = 0;
for(int i = 1; ;i++){
if(a[i] >= n && a[i] <= m) {
b[k++] = a[i];
}
if(a[i] > m) break;
}
printf("%d",b[0]);
FORA(i,1,k) {
printf(" %d",b[i]);
}
printf("\n");
}
return 0;
}