fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int N;
  6. cout << "Enter number of students: ";
  7. cin >> N;
  8.  
  9. // First two students
  10. int prev1 = 0; // first student says 0
  11. int prev2 = 1; // second student says 1
  12.  
  13. if (N >= 1) cout << prev1 << " ";
  14. if (N >= 2) cout << prev2 << " ";
  15.  
  16. // Loop for remaining students
  17. for (int i = 3; i <= N; i++) {
  18. int current;
  19. if (prev1 == prev2)
  20. current = 0;
  21. else
  22. current = 1;
  23.  
  24. cout << current << " ";
  25.  
  26. // Update previous two
  27. prev1 = prev2;
  28. prev2 = current;
  29. }
  30.  
  31. cout << endl;
  32. return 0;
  33. }
Success #stdin #stdout 0.01s 5284KB
stdin
10
stdout
Enter number of students: 0 1 1 0 1 1 0 1 1 0