A high school has 1000 students and 1000 lockers. On the first day of school, th
ID: 3622054 • Letter: A
Question
A high school has 1000 students and 1000 lockers. On the first day of school, the principal plays the following game: She asks the first student to go and open all of the lockers. She then asks the second student to go and close all the even numbered lockers. The third student is asked to check every 3rd locker, if open, close it; if closed, open it. The forth student is asked to check every 4th locker, if open, close it; if closed, open it. The remaining students continue the game. In general, the nth student checks every nth locker. After all the students have taken their turn, some of the lockers are open and some are closed.Write a program that prompts the user to enter the number of lockers in a school. After the game is over, the program outputs the number of lockers open. Test run your program with the following input: 1000, 5000, 10000.
(Hint: consider locker number 100. This locker is visited by student numbers 1, 2, 4, 5, 10, 20, 25, 50, 100. These are positive divisors of 100. Similarly, locker number 30 is visited by student numbers 1, 2, 3, 5, 6, 10, 15, 30. Notice that if the number of positive divisors of a locker number is odd, then at the end of the game the locker is open. If the number of positive divisors of a locker number is even, then at the end of the game the locker is closed.)
Answer
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
int main()
{int lockers;
int locker[15000];
int i,j,open=0; //using the variable will make things easier later
cout<<"Enter the number of lockers ";
cin>>lockers;
for ( i=0; i < lockers; i++) //start with all lockers closed = 0; opened will be 1
locker[i]=0;
for(i=0;i<lockers;i++) //do this for every student
{
for(j=i;j<lockers;j+=i+1) //student 0 checks every locker, student 1 every other etc
locker[j]++; // even count is opened
}
for(i=0;i<lockers;i++)
{if(locker[i]%2==1) //odd count is closed
open++; //count it as open
}
cout<<" Total number of lockers open: "<<open<<endl<<endl;
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.