-
Notifications
You must be signed in to change notification settings - Fork 0
/
String Fibonacci.cpp
executable file
·53 lines (53 loc) · 1.06 KB
/
String Fibonacci.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
#include <bits/stdc++.h>
#define N 20000
using namespace std;
char x[N], dp[N][N+N];
char a[N], b[N];
void add(char a[], char b[]);
/*This is my summation function*/
void store_fib();
/*This function for store my Fibonacci*/
int main()
{
store_fib();
int n;
while(cin>>n){
cout<<dp[n-1]<<endl;
}
return 0;
}
void add(char a[], char b[])
{
int i, j, k, len, top_len;
top_len = strlen(a);
len = strlen(b);
memset(x,0,sizeof(x));
if(top_len<len)
return add(b,a);
int sum=0;
for(i=top_len-1,j=0,k=len-1; i>=0; i--, k--){
if(k<0) sum+=(a[i]-48);
else sum+=((a[i]-48)+(b[k]-48));
x[j++] = (sum%10)+48;
sum/=10;
}
if(sum>0) x[j++]=(sum+48);
for(i=0,k=j-1; i<=(j/2); i++,k--){
if(i<k){
char temp = x[i];
x[i] = x[k];
x[k] = temp;
}
}
}
void store_fib()
{
a[0]='1',b[0]='1';
for(int i=0; i<N; i++){
add(a,b);
/*Call for addition*/
strcpy(a,b);
strcpy(b,x);
strcpy(dp[i],a);
}
}