fork download
  1. // Level 1 - Homework 1 - Question Y - Solution 1.
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int N = 0;
  9. cin >> N;
  10.  
  11. if(N == 1) // I didn't put if(N == 0) because the question mentioned that N >= 1.
  12. cout << '0' << endl;
  13.  
  14. else
  15. {
  16. cout << "0 1 ";
  17.  
  18. N -= 2;
  19. int x = 0;
  20. int y = 1;
  21.  
  22. for(int i = 0; i < N; ++i)
  23. {
  24. cout << x + y << ' ';
  25.  
  26. // We need to make x = y, y = x + y.
  27. int Helper = y; // Helper = y before change it to x + y.
  28. y = x + y; // New y = Old y + x.
  29. x = Helper; // New x = Old y.
  30. }
  31. }
  32.  
  33. return 0;
  34. }
Success #stdin #stdout 0.01s 5292KB
stdin
7
stdout
0 1 1 2 3 5 8