Fibonacci

来源:互联网 发布:茶水浇花知乎 编辑:程序博客网 时间:2024/05/22 04:50

问题 C: Fibonacci

时间限制: 1 Sec
内存限制: 64 MB
提交: 28
解决: 12
提交状态

题目描述

In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

An alternative formula for the Fibonacci sequence is

.

Given an integer n, your goal is to compute the last 4 digits of Fn.

输入

The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤ n ≤ 1,000,000,000). The end-of-file is denoted by a single line containing the number −1.

输出

For each test case, print the last four digits of Fn. If the last four digits of Fn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print Fn mod 10000).

样例输入

099999999991000000000-1

样例输出

0346266875
#include <stdio.h>int main(){int n,k1,k2,k3,i,k,sum,l,a[38324],h;while(scanf("%d",&n)!=EOF){if(n == (-1)){    break;    }else if(n<=2){printf("%d\n",n);}else{k1 = 0;k2 = 1;for(i = 3;i<=n+1;i++){k1 = k1 % 10000;
                k2 = k2 % 10000;
<pre name="code" class="html">                k3 = k1 + k2;k1 = k2;
                <span style="font-family: Arial, Helvetica, sans-serif;">k2 = k3;</span>
k3 = k3 % 10000; }printf("%d\n",k3); } }return 0;}

0 0
原创粉丝点击