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

猜你喜欢

转载自blog.csdn.net/qq_41398448/article/details/83042988