Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- void fillZero(string &a, string &b)
- {
- int lengthMax = max(a.length(), b.length());
- while (a.length() < lengthMax) a = '0' + a;
- while (b.length() < lengthMax) b = '0' + b;
- }
- int SoSanh(string a, string b)
- {
- if (a.length() > b.length()) return 1;
- else if (a.length() < b.length()) return -1;
- for (int i = 0; i < a.length(); i++)
- {
- int x = a[i] - 48;
- int y = b[i] - 48;
- if (x > y) return 1;
- else if (x < y )return -1;
- }
- return 0;
- }
- string Cong(string a, string b)
- {
- int du = 0;
- string res = "";
- fillZero(a, b);
- for (int i = a.length() - 1; i >= 0; i--)
- {
- int x = a[i] -48;
- int y = b[i] -48;
- int temp = x + y + du;
- if ( i == 0)
- {
- stringstream ss;
- string s;
- ss << temp;
- ss >> s;
- res = s + res;
- break;
- }
- res = (char)(temp % 10 + 48) + res;
- du = temp / 10;
- }
- while (res.length() > 1 && res[0] == '0') res.erase(0, 1);
- return res;
- }
- string genFibo(int n)
- {
- string f1 = "1", f2 = "1", f3 = "2";
- if (n == 1 || n == 2 ) return f1;
- for (int i = 3; i <= n; i++)
- {
- f3 = Cong(f1, f2);
- f1 = f2;
- f2 = f3;
- }
- return f3;
- }
- int main()
- {
- cin.tie(0);
- ios_base::sync_with_stdio(0);
- int n;
- cin >> n;
- cout << genFibo(n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement