Write a program that solves a Locker puzzle which says the following; The school
ID: 3760822 • Letter: W
Question
Write a program that solves a Locker puzzle which says the following; The school has 100 students also 100 lockers. The first day of school all lockers are closed. Once students enter the building, the first student will open every locker. Then the second student will start with the second locker and begin to close every other locker. The third student so on will begin with the third locker and changes all the others if they were opened or closed. This will continue on until the 100th student changes his own locker. Once school is over, what lockers will be still open? This problem can use a list of 100 Boolean Elements. True for open, false for closed. This program should be made using Python. Write a program that solves a Locker puzzle which says the following; The school has 100 students also 100 lockers. The first day of school all lockers are closed. Once students enter the building, the first student will open every locker. Then the second student will start with the second locker and begin to close every other locker. The third student so on will begin with the third locker and changes all the others if they were opened or closed. This will continue on until the 100th student changes his own locker. Once school is over, what lockers will be still open? This problem can use a list of 100 Boolean Elements. True for open, false for closed. This program should be made using Python.Explanation / Answer
please rate - thanks
#include <stdio.h>
#include <conio.h>
int main()
{int students;
int locker[100];
int i,j,open=0; //using the variable will make things easier later
students=100;
for(i=0;i<students;i++) //start with all lockers closed = 0; opened will be 1
locker[i]=0;
for(i=0;i<students;i++) //do this for every student
{for(j=i;j<students;j+=i+1) //student 0 checks every locker, student 1 every other etc
locker[j]++; // even count is opened
}
printf("The open lockers are ");
for(i=0;i<students;i++)
{if(locker[i]%2==1) //odd count is closed
{open++; //count it as open
printf("%d ",i+1); //add 1 since started at 0
if(open%10==0) //print ten lockers per line
printf(" ");
}
}
printf(" Total number of lockers open: %d ",open);
printf("The closed lockers are ");
open=0;
for(i=0;i<students;i++)
{if(locker[i]%2==0) //odd count is closed
{open++; //count it as closed
printf("%d ",i+1); //add 1 since started at 0
if(open%10==0) //print ten lockers per line
printf(" ");
}
}
printf(" Total number of lockers closed: %d ",open);
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.