1. 程式人生 > >zcmu 1095

zcmu 1095

包括 cci \n sin input scrip map nac 遍歷

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;
}

zcmu 1095