-
Notifications
You must be signed in to change notification settings - Fork 6
/
0110SS.cpp
115 lines (110 loc) · 2.66 KB
/
0110SS.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
#define SIZE 2100
class Integer {
char num[SIZE], sign;
long long size, capacity;
void initializeStr(int limit) {
for(int i = 0; i < limit; i++)
num[i] = '0';
}
public:
Integer() : size(0), capacity(0) {
capacity = SIZE;
sign = '+';
initializeStr(capacity);
}
Integer(std::string aStr) : size(aStr.length()), capacity(0) {
capacity = SIZE;
if(aStr.find('-') != std::string::npos) {
sign = '-';
}
else
sign = '+';
initializeStr(capacity);
for(int i = capacity-1, j = aStr.length()-1; i >= 0 && j >= 0; j--, i--) {
num[i] = aStr[j];
}
}
Integer(const Integer& a) {
capacity = a.getCapacity();
for(int index = 0; index < capacity; index++) {
num[index] = a.getChar(index);
}
sign = a.getSign();
size = a.getSize();
}
int getDigit(int index) const {
return num[index] - '0';
}
char getChar(int index) const { return num[index]; }
char getSign() const { return sign; }
long long getSize() const { return size; }
long long getCapacity() const { return capacity; }
void findDigits() {
int i = 0;
while(i < SIZE && num[i] == '0') i++;
size = capacity - i;
if(size == 0) // In case of zero
size = 1;
}
friend std::ostream& operator<<(std::ostream& tmp, const Integer& a) {
int i = 0;
while(i < SIZE && a.num[i] == '0') i++;
if(i == SIZE)
tmp << 0;
else {
while(i < SIZE) {
tmp << a.num[i++];
}
}
return tmp;
}
friend Integer operator+(const Integer& a, const Integer& b) {
int index, tmp = 0, carry = 0;
Integer C;
for(int i = a.getCapacity()-1; i >= 0; i--) {
tmp = a.getDigit(i) + b.getDigit(i) + C.getDigit(i);
carry = tmp / 10;
tmp = tmp % 10;
C.num[i] = tmp + '0';
if(i > 0) {
C.num[i-1] = C.num[i-1] + carry;
}
}
C.findDigits();
return C;
}
void set_integer(const char buffer[]) {
int buffer_index = strlen(buffer)-1, index = SIZE - 1;
sign = (buffer[0] == '-') ? '-' : '+';
while(buffer_index >= 0) {
if(buffer[buffer_index] != '-') {
num[index] = buffer[buffer_index];
buffer_index --;
index --;
}
else break;
}
size = (buffer[0] == '-') ? strlen(buffer) - 1 : strlen(buffer);
}
};
int main() {
int N;
cin >> N;
if(N <= 2)
cout << N+1 << endl;
else {
Integer f1(string("2")), f2(string("3"));
Integer result;
for(int i = 0; i < N-2; i++) {
result = f1 + f2;
f1 = f2;
f2 = result;
}
cout << result << endl;
}
return 0;
}