Pagini recente » Cod sursa (job #114258) | Cod sursa (job #2905007) | Cod sursa (job #2725498) | Cod sursa (job #27859) | Cod sursa (job #2469290)
#include <fstream>
#include <iostream>
// 999th Fibonacci number has 209 digits, by the way,
// so I'm '''rounding''' that to 300.
typedef int big_nr[300];
unsigned N;
big_nr first, sec;
// Adds two big numbers;
// Important: the result is stored in 'x'!!!!
void Add(big_nr x, big_nr y)
{
unsigned t = 0;
if (x[0] < y[0])
{
x[0] = y[0];
}
for (unsigned i = 1; i <= x[0]; ++i, t /= 10)
{
t = x[i] + y[i] + t;
x[i] = t % 10;
}
if (t == 1)
{
x[++x[0]] = t;
}
}
// Function that returns the 'q' Fibonacci number;
// it also works for Fibonacci numbers higher than 2^64 - 1.
unsigned big_fib(unsigned q)
{
// initialise our 'big_nr's inside the 'big_fib' function, via
// the 'initialise' function (via lambdas).
auto initialise = []()
{
first[0] = sec[0] = 1;
// Here, we define the first Fibonacci number to be 1,
// and the second Fibonacci number to be 2.
first[1] = 1, sec[1] = 2;
};
initialise();
// Current index of the fib. nr.
// The cases for q = 1 and q = 2 have (already) been solved.
unsigned index = 2;
while (index < q)
{
index += 2;
Add(first, sec);
Add(sec, first);
}
return index;
}
// Function that reads the input.
void read()
{
std::ifstream fisierIN("nunta.in");
fisierIN >> N;
fisierIN.close();
}
// Function that displays the result (i.e. writes the
// result to the requested folder).
void display()
{
unsigned val = big_fib(N);
if (val == N)
{
std::ofstream fisierOUT("nunta.out");
for (unsigned i = sec[0]; i >= 1; --i)
{
fisierOUT << sec[i];
}
fisierOUT.close();
}
else
{
std::ofstream fisierOUT("nunta.out");
for (unsigned i = first[0]; i >= 1; --i)
{
fisierOUT << first[i];
}
fisierOUT.close();
}
}
int main()
{
read();
display();
return 0;
}