Subject description:
-
We all know that Fibonacci number, and now asked to enter an integer n, you export item n Fibonacci Fibonacci number sequence. Fibonacci series deed defined as follows:
Input:
-
Input may contain multiple test samples for each test case,
Input comprises an integer n (1 <= n <= 70).
Output:
-
Corresponding to each test case,
The n-th item value Qi Fibonacci sequence.
- Sample input:
- 3
Sample output:
- 2
Problem-solving ideas:
- The title can be seen, n ranges from 70, the first thought is certainly not recursive, then we drill a loophole in advance to generate an array, to take the time out on it. The idea is too high, can not think, I remember there is a method used to solve the matrix, and soon. This question is just not time out on it.
- Also must pay attention to, it is the big recursive data 70 when, remember to use the largest type of long long% lld output can !
Code:
#include <stdio.h>
long long num[71] = {0,1};
void createfi(long long *num);
int main(void){
int n;
createfi(num);
while(scanf("%d",&n)!=EOF && n>=1 && n<=70){
printf("%lld\n",num[n]);
}
return 0;
}
void createfi(long long *num){
int i;
for(i=2;i<71;i++){
num[i] = num[i-1]+num[i-2];
}
}
/**************************************************************
Problem: 1387
User: xhalo
Language: C
Result: Accepted
Time:0 ms
Memory:916 kb
****************************************************************/
Reproduced in: https: //my.oschina.net/u/204616/blog/545095