Cod sursa(job #1659496)

Utilizator depevladVlad Dumitru-Popescu depevlad Data 22 martie 2016 11:53:33
Problema Nunta Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <cstdio>

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

        res.x[0] = (x[0] > other.x[0] ? 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--) {
            printf("%d", x[i]);
        }
    }
};

int main() {
    freopen("nunta.in", "r", stdin);
    freopen("nunta.out", "w", stdout);

    int n, i;

    scanf("%d", &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;
}