summaryrefslogtreecommitdiff
path: root/cpp/lswf.cpp
blob: ee7d96facadc0557a9554eb84609a083baffc6b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* INPUT:
* 19		
*		
* OUTPUT:		
* 1000101
*/
#include <iostream>
#include <fstream>
#include <list>
#define MAXG 1000001

int fibonacci(int* fib, int N)
{
    fib[0] = 1; fib[1] = 1;
    int lst;

    for(int i = 2; i < N; i++) {
        fib[i] = fib[i-1] + fib[i-2];
        lst = i;
        if(fib[i] > N) break;
    }

    return lst;
}

int main()
{
    std::ifstream in("input.txt");
    std::ofstream out("output.txt");
    std::list<int> seq;
    std::list<int>::iterator j;

    int N, i;
    in >> N;
    int caracts[MAXG], somma = 0, potSomma;
    int lastc;

    if(N > 4)
        lastc = fibonacci(caracts, N);
    else {
        caracts[0] = 1;
        for(i = 1; i < 4; i++)
            caracts[i] = i;

        switch (N) {
            case 1:
            case 2:
            case 3:
                lastc = N;
                break;
            default:
                lastc = 4;
        }
    }

    for(i = lastc; i > 0; i--) {
        potSomma = somma + caracts[i];
        if(potSomma < N) {
            somma = potSomma;
            seq.push_front(1);
        } else seq.push_front(0);
    }
    seq.push_front(1);
    for(j = seq.begin(); j != seq.end(); j++) out << *j;

    in.close();
    out.close();
    return 0;
}