-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path25_1000_digit_fibonnacci.cpp
103 lines (96 loc) · 1.48 KB
/
25_1000_digit_fibonnacci.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
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
typedef vector<int> vi;
vi operator+(const vi& lhs, const vi& rhs);
vi& operator++(vi& v);
ostream& operator<<(ostream& os, const vi& rhs);
int main()
{
vi a(1,1);
vi b(1,1);
vi c=a+b;
int i=3;
while(c.size()!=1000)
{
c = a+b;
a = b;
b = c;
i++;
}
cout<<i<<endl;
}
vi& operator++(vi& v)
{
int carry = 1,n=v.size();
for(int i=0;carry && i<n;i++)
{
v[i]+=carry;
if(v[i] == 10)
{
v[i] = 0;
carry = 1;
}
else carry = 0;
}
if(carry)
v.push_back(1);
return v;
}
ostream& operator<<(ostream& os, const vi& rhs)
{
int i,n=rhs.size();
for(i=n-1;i>=0;i--)
os<<rhs[i];
return os;
}
vi operator+(const vi& lhs, const vi& rhs)
{
vi c(0,1);
int carry=0;
int result;
vi::const_iterator i=lhs.begin(),j=rhs.begin();
while(i<lhs.end() && j<rhs.end())
{
result = carry;
result += *(i++) + *(j++);
if(result>9)
{
carry = 1;
result = result%10;
}
else
carry = 0;
c.push_back(result);
}
while(i<lhs.end())
{
result = carry;
result += *(i++);
if(result>9)
{
carry = 1;
result = result%10;
}
else
carry = 0;
c.push_back(result);
}
while(j<rhs.end())
{
result = carry;
result += *(j++);
if(result>9)
{
carry = 1;
result = result%10;
}
else
carry = 0;
c.push_back(result);
}
if(carry)
c.push_back(carry);
return c;
}