fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios_base::sync_with_stdio(0);
  6. cin.tie(0);
  7. cout.tie(0);
  8.  
  9. int n;
  10. cin >> n;
  11.  
  12. // Fibonacci ardıcıllığının başlanğıc dəyərləri
  13. long long a = 0, b = 1;
  14.  
  15. // Fibonacci ardıcıllığının n-ci elementini hesablamaq
  16. for (int i = 2; i <= n; ++i) {
  17. long long next = a + b;
  18. a = b;
  19. b = next;
  20. }
  21.  
  22. // İlk iki element üçün xüsusi hal
  23. if (n == 1) {
  24. cout << 0 << "\n"; // a₀ = 0
  25. } else if (n == 2) {
  26. cout << 1 << "\n"; // a₁ = 1
  27. } else {
  28. cout << b << "\n"; // n > 2 üçün b Fibonacci ardıcıllığının n-ci elementidir
  29. }
  30.  
  31. return 0;
  32. }
  33.  
Success #stdin #stdout 0.01s 5288KB
stdin
8
stdout
21