Cod sursa(job #1659479)

Utilizator depevladVlad Dumitru-Popescu depevlad Data 22 martie 2016 11:15:06
Problema Nunta Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <fstream>

using namespace std;

ifstream in("nunta.in");
ofstream out("nunta.out");

class hugeInt {
private:
    static const int NDIG = 1005;
    int x[NDIG];
public:
    hugeInt(int k) {
        fill(x, x+NDIG, 0);
        do {
            x[++x[0]] = k % 10;
            k /= 10;
        } while(k);
    }
    hugeInt() {
        fill(x, x+NDIG, 0);
        x[0] = 1;
    }
    hugeInt operator +(const hugeInt &other) const {
        hugeInt res;
        int i, t;

        res.x[0] = max(x[0], other.x[0]);
        for(i = 1, t = 0; i <= res.x[0]; i++) {
            res.x[i] = x[i] + other.x[i] + t;
            t = res.x[i] / 10;
            res.x[i] %= 10;
        }
        if(t) {
            res.x[++res.x[0]] = t;
        }

        return res;
    }
    void print() {
        for(int i = x[0]; i > 0; i--) {
            out << x[i];
        }
    }
};

int main() {
    int n, i;

    in >> n;

    hugeInt f1(1), f2(2), f3(3);
    for(i = 4; i <= n; i++) {
        f1 = f2;
        f2 = f3;
        f3 = f3 + f1;
    }

    if(n == 1) f3 = f1;
    if(n == 2) f3 = f2;

    f3.print();
    return 0;
}