USTC OJ — 1003 Fibonacci Numbers(组合数学, 简单题)

1. 题目描述

Fibonacci数列定义为:

A0 = 0, A1 = 1;

An = An-1 + An-2, if n >= 2.

问题:输入一个n( 0 ≤ n ≤ 40 ),计算An.

2. 算法设计

直接利用公式计算即可,时间复杂度O(N),由于数据规模很小,可以选择

  1. 先打表,然后对每一个测试数据n,直接输出An;
  2. 或者分别对每一个n,计算An。

3. AC Code

 1 #include <stdio.h>
 2 #define N 41
 3 int f[N];
 4 void pre_do();
 5 void ac_fun();
 6 int main()
 7 {
 8     //freopen("in.txt", "r", stdin);
 9     pre_do();
10     ac_fun();
11     return 0;
12 }
13 void pre_do()
14 {
15     int i;
16     f[0] = 0; f[1] = 1;
17     for (i = 2;i < N;i++) f[i] = f[i-1] + f[i-2];
18 }
19 void ac_fun()
20 {
21     int n;
22     while(scanf("%d", &n) != EOF)
23     {
24         printf("The Fibonacci number for %d is %d\n", n, f[n]);
25     }
26 }

时间: 2024-08-10 21:15:01

USTC OJ — 1003 Fibonacci Numbers(组合数学, 简单题)的相关文章

zoj Fibonacci Numbers ( java , 简单 ,大数)

题目 //f(1) = 1, f(2) = 1, f(n > 2) = f(n - 1) + f(n - 2) import java.io.*; import java.util.*; import java.math.*; public class Main { /** * @xqq */ public BigInteger an(int n) { BigInteger c; BigInteger a = BigInteger.valueOf(1); BigInteger b = BigIn

___________一个简单题带来的启示____________________________

Problem Description There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2). Input Input consists of a sequence of lines, each containing an integer n. (n < 1,000,000). Output Print the word "yes" if

poj2105 IP Address(简单题)

题目链接:http://poj.org/problem?id=2105 Description Suppose you are reading byte streams from any device, representing IP addresses. Your task is to convert a 32 characters long sequence of '1s' and '0s' (bits) to a dotted decimal format. A dotted decima

codeforces 446C DZY Loves Fibonacci Numbers(数学 or 数论+线段树)

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation F1 = 1; F2 = 1; Fn = Fn - 1 + Fn - 2 (n > 2). DZY loves Fibonacci numbers very much. Today DZY gives you an array consisting of n integers: a1, a2, ...,

NYOJ 663 弟弟的作业【简单题更能体现水平。。。】

弟弟的作业 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描述 你的弟弟刚做完了"100以内数的加减法"这部分的作业,请你帮他检查一下.每道题目(包括弟弟的答案)的格式为a+b=c或者a-b=c,其中a和b是作业中给出的,均为不超过100的非负整数:c是弟弟算出的答案,可能是不超过200的非负整数,也可能是单个字符"?",表示他不会算. 输入 输入文件包含不超过100行,以文件结束符结尾.每行包含一道题目,格式保证符合上述规定,且不包含任何

九度OJ 1092 Fibonacci

题目1092:Fibonacci 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1576 解决:1158 题目描述: The Fibonacci Numbers{0,1,1,2,3,5,8,13,21,34,55...} are defined by the recurrence: F0=0 F1=1 Fn=Fn-1+Fn-2,n>=2 Write a program to calculate the Fibonacci Numbers. 输入: Each case contains

hdu 4970 Killing Monsters(简单题) 2014多校训练第9场

Killing Monsters                                                                        Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Kingdom Rush is a popular TD game, in which you should b

1003 - Sibonacci Numbers

1003 - Sibonacci Numbers Time Limit: 1s Memory Limit: 64MB Submissions: 2130 Solved: 360 Description As is known to all, the definition of Fibonacci Numbers is: f(1)=1 f(2)=1 f(n)=f(n-1)+f(n-2) (n>=3) Now Sempr found another Numbers, he named it "

HDU 3117 Fibonacci Numbers(斐波那契前后四位,打表+取对+矩阵快速幂)

HDU 3117 Fibonacci Numbers(斐波那契前后四位,打表+取对+矩阵快速幂) ACM 题目地址:HDU 3117 Fibonacci Numbers 题意: 求第n个斐波那契数的前四位和后四位. 不足8位直接输出. 分析: 前四位有另外一题HDU 1568,用取对的方法来做的. 后四位可以用矩阵快速幂,MOD设成10000就行了. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * Blog: http://blog.csdn.