Advertisement
Guest User

Shorter

a guest
Apr 6th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>
  4.  
  5. using namespace std;
  6.  
  7. /*
  8. In an imaginary high school there exist 1000 lockers labelled 1, 2, ..., 1000.
  9. All of them are closed. 1000 students are to "toggle" a locker's state.
  10. * The first student toggles all of them * The second one toggles every other
  11. one (i.e, 2, 4, 6, ...) * The third one toggles the multiples of 3 (3, 6, 9, ...)
  12. and so on until all students have finished.
  13.  
  14. To toggle means to close the locker if it is open, and to open it if it's closed.
  15.  
  16. How many and which lockers are open in the end?
  17. */
  18.  
  19. int main()
  20. {
  21.  
  22.     int temp = 1;
  23.     for( int x = 1; x != 1001; x++ )
  24.     {
  25.         for( int y = 1; y <= x; y++ )
  26.         {
  27.             if( x % y == 0 )
  28.             {
  29.                 temp++;
  30.             }
  31.         }
  32.         if( temp % 2 == 0 )
  33.         {
  34.             cout << x << ", ";
  35.         }
  36.         temp = 1;
  37.     }
  38.  
  39.     cout << "\n\nDone.\n\n";
  40.     cin.get();
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement