URI Online Judge Solution 1176 Array change I Using C, Python Programming Language.
Write a program that reads a number and print the Fibonacci number corresponding to this read number. Remember that the first elements of the Fibonacci series are 0 and 1 and each next term is the sum of the two preceding it. All the Fibonacci numbers calculated in this program must fit in a unsigned 64 bits number.
Input
The first line of the input contains a single integer T, indicating the number of test cases. Each test case contains a single integer N (0 ≤ N ≤ 60), corresponding to the N-th term of the Fibonacci series.
Output
For each test case in the input, print the message "Fib(N) = X", where X is the N-th term of the Fibonacci series.
Input Sample | Output Sample |
3 | Fib(0) = 0 |
Solution Using C:
#include <stdio.h>
int main() {
int T,i,n;
long long x,y,t,Fib[61];
scanf("%d",&T);
x = 0;
y = 1;
for(i=0; i<=61; i++)
{
if(i==0)
{
Fib[i] = 0;
}
else if(i==1)
{
Fib[i] = 1;
}
else
{
t = x + y;
Fib[i] = t;
x = y;
y = t;
}
}
while(T--)
{
scanf("%d",&n);
printf("Fib(%d) = %lld\n",n,Fib[n]);
}
return 0;
}
int main() {
int T,i,n;
long long x,y,t,Fib[61];
scanf("%d",&T);
x = 0;
y = 1;
for(i=0; i<=61; i++)
{
if(i==0)
{
Fib[i] = 0;
}
else if(i==1)
{
Fib[i] = 1;
}
else
{
t = x + y;
Fib[i] = t;
x = y;
y = t;
}
}
while(T--)
{
scanf("%d",&n);
printf("Fib(%d) = %lld\n",n,Fib[n]);
}
return 0;
}
Comments
Post a Comment