Fibonacci數列練習題
以下練習題 出自此網頁 http://cpp.zjut.edu.cn/ProblemList.aspx
1090 菲波那契數的餘數
Time Limit:1000MS Memory Limit:32768K
Description:
菲波那契數大家可能都已經很熟悉了: f(1)=0; f(2)=1; f(n)=f(n-1)+f(n-2) n>2。 因此,當需要其除以某個數的餘數時,不妨加一些處理就可以得到。
Input:
輸入資料為一些整數對P、K,P(1<P<5000),表示菲波那契數的序號,K(1<=K<15)表示2的冪次方。遇到兩個空格隔開的0時表示結束處理。
Output:
輸出其第P個菲波那契數除以2的K次方的餘數。
Sample Input:
6 2
20 10
0 0
Sample Output:
1
85
#include<stdio.h> #include<time.h> #include<math.h> int tailfib(int n,int acc1,int acc2) { if(n==1) return 0; if (n ==2) { return acc1; } return tailfib(n-1,acc2,acc1 + acc2); } void main() { int P,K; int x; while(scanf("%d%d",&P,&K)!=0&&P!=0&&P!=0){ int t=((int)pow(2,K)); printf("%d mod %d=",tailfib(P,1,1),t); x=tailfib(P,1,1)%t; printf("%d\n",x); } }
1091 Fib數之奇偶性
Time Limit:1000MS Memory Limit:32768K
Description:
Fibonacci數列定義如下: a[1]=1; a[2]=1; a[n]=a[n-1]+a[n-2](n>2)。 對於給定N (1≤N≤10000),請判別數列第N項的奇偶性。
Input:
給定整數N,如N=0則結束輸入(N=0不需要判斷)。
Output:
輸出第N項Fibonacci數列的奇偶性,如果為奇數則請輸出“ODD”,否則為“EVEN”。
Sample Input:
1 2 3 0
Sample Output:
ODD
ODD
EVEN
#include<stdio.h>
#include<time.h>
#include<math.h>
int tailfib(int n,int acc1,int acc2) {
if (n < 2) {
return acc1;
}
return tailfib(n-1,acc2,acc1 + acc2);
}
void main()
{
int N;
while(scanf("%d",&N)&&N!=0){
if(tailfib(N,1,1)%2!=0)
printf("ODD\n");
else
printf("EVEN\n");
}
}
1211 菲波那契數
Time Limit:1000MS Memory Limit:32768K
Description:
已知菲波那契數的定義: f(0) = 0 f(1) = 1 f(n) = f(n-1) + f(n-2) n>1的整數 根據輸入資料中的n,輸出第n項菲波那契數。
Input:
輸入資料中含有一些整數n(0≤n≤46)。
Output:
根據每個整數n,輸出其第n項菲波那契數,每個數佔獨立一行。
Sample Input:
5
6
7
8
9
40
Sample Output:
5
8
13
21
34
102334155
#include<stdio.h>
#include<time.h>
#include<math.h>
double tailfib(int n,int acc1,int acc2) {
if (n < 1) {
return acc1;
}
return tailfib(n-1,acc2,acc1 + acc2);
}
void main()
{
int N;
printf("需要輸出第幾個菲波那契數?\n");
while(scanf("%d",&N)!=0)
printf("第%d個斐波那契數:%0.0lf\n",N,tailfib(N,0,1));
}
1451 Fib數的2冪次模
Time Limit:200MS Memory Limit:32768K
Description:
已知菲波那契(簡寫為Fib)函式對於正整數構成一個數列。 f(0)=1,f(1)=1,f(n)=f(n-1)+f(n-2)。(n>1) 2的冪次方有2,4,8,…,第n項Fib數對於2的冪次方的餘數是多少,是你現在要關心的問題。
Input:
輸入資料有若干整數對,每對整數m,n表示2的m(1≤m≤32)次冪和第n(0<n<400000)項Fib數。
Output:
對於每對整數m,n,輸出第n項Fib數對於2的m次方的餘數。
Sample Input:
2 2
2 3
5 28
Sample Output:
2
3
21
#include<stdio.h>
#include<math.h>
int Fibonacci(int n) //迭代法
{
int F,Fa,Fb;
Fa=1;Fb=1;
for(int i=2;i<=n;i++){
F=Fa+Fb;
Fb=Fa;
Fa=F;
}
return F;
}
void main()
{
int n,m;
int sum;
while(scanf("%d %d",&m,&n)!=0){
sum=Fibonacci(n)%(int)pow(2,m);
printf("%d\n",sum);
}
}
1457 Fibonacci again
Time Limit:1000MS Memory Limit:32768K
Description:
A Fibonacci sequence is calculated by adding the previous two members in the sequence,with the first two members being both 1.Namely,f(1)=1,f(2)=1,f(n>3)=f(n-1)+f(n-2).
Your task is to take a number n as input,and print the result of that Fibonacci mod five.
Input:
Each line will contain an integer n(n<=10^9).Process to end of file.
Output:
For each case,output the result in a line.
Sample Input:
1
5
Sample Output:
1
0
#include<stdio.h>
#include<time.h>
#include<math.h>
int tailfib(int n,int acc1,int acc2) {
if (n < 2) {
return acc1;
}
return tailfib(n-1,acc2,acc1 + acc2);
}
void main()
{
int N;
int sum;
while(scanf("%d",&N)!=0&&N!=0){
sum=tailfib(N,1,1)%5;
printf("輸出斐波那契%d模五的結果%d\n",N,sum);
}
}
1500 Fibonacci Number
Time Limit:1000MS Memory Limit:32768K
Description:
The fibonacci numbers are as follows:
f[1] = 1; f[2] = 2;
f[n] = f[n - 1] + f[n - 2];
And s[n] is defined:
s[n]=f[1]*f[1]+f[2]*f[2]+…+f[n]*f[n]
Now ,give you the integer number a, x and n, you should calculate the ans, and the ans is defined as follows:
ans = (a^s[x]) % n;
You must pay attention to the range of the number: 1 ≤ a ≤ 100000000; 1 ≤ x ≤ 1000000000; 2 ≤ n ≤ 100000000.
Input:
The input contains several test cases. For each test case, only line contains three integer numbers a, x and n separated by spaces. The end of the input is indicated by a line containing three zeros separated by spaces.
Output:
For each test case the output is only one integer number ans in a line.
Sample Input:
1 1 100
2 3 1000000
0 0 0
Sample Output:
1
16384
1520 等差數列&Fibonacci數列
Time Limit:5000MS Memory Limit:32768K
Description:
童鞋們已經對等差數列和Fibonacci數列非常瞭解了。
所謂等差數列就是:
g(i)=k*i+b;
我們在這裡假設k,b為非負整數。
所謂Fibonacci數列就是:
f(0)=0
f(1)=1
f(n)=f(n-1)+f(n-2) (n>=2)
現在小戴童鞋心中總有個問題無法解決:
給你k,b,n,請你計算f(g(i)) 的和,其中i為[0,n),結果模取M。
Input:
資料存在多組,每組一行,有四個非負整數k,b,n,M。 每個數都不超過1,000,000,000。
Output:
輸出所要求得結果,每個結果佔一行。
Sample Input:
2 1 4 100
2 0 4 100
Sample Output:
21
12
#include<stdio.h>
#include<time.h>
long tailfib(int n,int acc1,int acc2) {
if(n==0) return 0;
if (n < 2) {
return acc1;
}
return tailfib(n-1,acc2,acc1 + acc2);
}
void main()
{
int i,g;
int k,b,n,M;
while(scanf("%d%d%d%d",&k,&b,&n,&M)!=0){
int s=0;
for(i=0;i<n;i++){
g=k*i+b;
s+=(tailfib(g,1,1));
}
printf("%d\n",s%M);
}
}