Pagini recente » Cod sursa (job #2355331) | Cod sursa (job #1041500) | Cod sursa (job #470664) | Cod sursa (job #1742045) | Cod sursa (job #960131)
Cod sursa(job #960131)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
std::ifstream in ("nunta.in");
std::ofstream out ("nunta.out");
class largeInt
{
public:
largeInt(unsigned long long n);
largeInt& operator=(const unsigned long long &rhs);
largeInt& operator+=(const largeInt &rhs);
largeInt& operator+=(const unsigned long long &rhs);
largeInt operator+(const largeInt &rhs) const;
largeInt operator+(const unsigned long long &rhs) const;
largeInt& operator-=(const largeInt &rhs);
largeInt& operator-=(const unsigned long long &rhs);
largeInt operator-(const largeInt &rhs) const;
largeInt operator-(const unsigned long long &rhs) const;
bool operator==(const largeInt &rhs) const;
bool operator!=(const largeInt &rhs) const;
bool operator<(const largeInt &rhs) const;
bool operator<=(const largeInt &rhs) const;
bool operator>(const largeInt &rhs) const;
bool operator>=(const largeInt &rhs) const;
friend std::ostream& operator<<(std::ostream& os, const largeInt& n);
friend std::istream& operator>>(std::istream& is, largeInt& n);
private:
void fixOverflow();
void fixUnderflow();
std::vector<int> nr;
};
largeInt::largeInt(unsigned long long n)
{
while(n!=0)
{
this->nr.push_back(n%10);
n/=10;
}
}
largeInt& largeInt::operator=(const unsigned long long &rhs)
{
unsigned long long n = rhs;
while(!nr.empty())nr.pop_back();
while(n!=0)
{
this->nr.push_back(n%10);
n/=10;
}
return *this;
}
void largeInt::fixOverflow()
{
for(int i = 0; i < nr.size()-1; i++)
if(nr[i]>=10)
{
int aux = nr[i]%10;
nr[i+1]=nr[i]/10;
nr[i]=aux;
}
if(nr[nr.size()-1]>=10)
{
int i = nr.size()-1;
while(nr[i]>=10)
{
nr.push_back(nr[i]%10);
nr[i]/=10;
}
}
}
void largeInt::fixUnderflow()
{
for(int i = 0; i < nr.size(); i++)
{
if(nr[i]<0)
{
nr[i+1]--;
nr[i]+=10;
}
}
while(nr.back()==0)
nr.pop_back();
}
largeInt& largeInt::operator+=(const largeInt &rhs)
{
if(this->nr.size()>rhs.nr.size())
{
for(int i = 0; i < rhs.nr.size(); i++)
this->nr[i]+=rhs.nr[i];
}
else
{
for(int i = 0; i < this->nr.size(); i++)
this->nr[i]+=rhs.nr[i];
for(int i = this->nr.size(); i < rhs.nr.size(); i++)
this->nr.push_back(rhs.nr[i]);
}
fixOverflow();
}
largeInt& largeInt::operator+=(const unsigned long long &rhs)
{
largeInt aux(rhs);
*this+=aux;
return *this;
}
largeInt largeInt::operator+(const largeInt &rhs) const
{
largeInt aux = *this;
aux+=rhs;
return aux;
}
largeInt largeInt::operator+(const unsigned long long &rhs) const
{
largeInt aux = *this;
aux+=rhs;
return aux;
}
largeInt& largeInt::operator-=(const largeInt &rhs)
{
for(int i = 0; i < rhs.nr.size(); i++)
this->nr[i]-=rhs.nr[i];
fixUnderflow();
return *this;
}
bool largeInt::operator==(const largeInt &rhs) const
{
if(this->nr.size()!=rhs.nr.size()) return false;
else
{
for(int i = 0; i < rhs.nr.size(); i++)
if(rhs.nr[i]!=this->nr[i])return false;
return true;
}
}
bool largeInt::operator!=(const largeInt &rhs) const
{
return !(*this==rhs);
}
bool largeInt::operator<(const largeInt &rhs) const
{
if(this->nr.size()<rhs.nr.size())return true;
if(this->nr.size()>rhs.nr.size())return false;
for(int i = rhs.nr.size()-1; i >= 1; i--)
if(this->nr[i]>rhs.nr[i])return false;
if(this->nr[0]>=rhs.nr[0])return false;
return true;
}
bool largeInt::operator>=(const largeInt &rhs) const
{
return !(*this<rhs);
}
bool largeInt::operator<=(const largeInt &rhs) const
{
return (*this<rhs && *this==rhs);
}
bool largeInt::operator>(const largeInt &rhs) const
{
return !(*this<=rhs);
}
std::ostream& operator<<(std::ostream& os, const largeInt& n)
{
for(int i = n.nr.size()-1; i >= 0; i--)
os << n.nr[i];
return os;
}
std::istream& operator>>(std::istream& is, largeInt& n)
{
while(!n.nr.empty())n.nr.pop_back();
while(is.peek()>'9'&&is.peek()<'0')is.ignore();
while(is.peek()<='9'&&is.peek()>='0')
{
char t;
is >> t;
n.nr.push_back(t-'0');
}
std::reverse(n.nr.begin(), n.nr.end());
return is;
}
int main()
{
largeInt f1(1);
largeInt f2(1);
int n;
in >> n;
for(int i = 0; i < n-1; i++)
{
largeInt aux(0);
aux = f1+f2;
f1=f2;
f2=aux;
}
out << f2 << '\n';
}