Question
I need help writing a program for this assignment

Problem: Locker puzzle A school has 100 lockers and 100 students. All lockers are locked on the first day of school. As the students enter, the 1st student opens every locker. Then the 2nd student, begins with the 2nd locker, closes every other locker. The 3rd student begins with the 3rd locker and changes every 3rd locker (closes it if its open and opens it if its closed). The 4th student begins with the 4th locker and changes every 4th locker. The 5th students begins with 5h locker and changes every 5th locker, and so on, until the 100th student changes the 100th locker.
media%2F5a5%2F5a536597-d5fc-4a54-90aa-74
0 0
Add a comment Improve this question Transcribed image text
Answer #1

This is easy. Lets define the state of the locker as open or closed. At any given point in time we know that a locker will be in one of these two states. Lets say we denote open state by 1 and closed state by 0. so we have to print all the lockers with open state which is 1 at the end of the task. We can do this in the following way.

Code :

#include <iostream>

using namespace std;

int main() {

//set all lockers state to closed

int lockers[101]={0};

for(int i = 1;i<=100;i++){

//we will start from ith locker for ith student

for(int j=i;j<=100;j=j+i){

//toggle the state of the lockers

lockers[j]=1-lockers[j];

}

}

for(int i =1;i<=100;i++){

if(lockers[i]==1){

cout<<i<<" ";

}

}

cout<<endl;

return 0;

}

OUTPUT :

1 4 9 16 25 36 49 64 81 100 
Add a comment
Know the answer?
Add Answer to:
I need help writing a program for this assignment Problem: Locker puzzle A school has 100...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA PLEASE Concepts tested by this program: Variables Input/output .Loops Conditional Statements Methods . Arrays ....

    JAVA PLEASE Concepts tested by this program: Variables Input/output .Loops Conditional Statements Methods . Arrays . Multi-Dimensional Arrays Program 1: Locker Puzzle Problem Description: A school has 100 lockers and 100 students. All lockers are closed on the first day of school. As the students enter, the first student, denoted S1, opens every locker. Then the second student, S2, begins with the second locker, denoted L2, and closes every other locker. Student S3 begins with the third locker and changes...

  • The Lockers Problem There is a well-known mathematical logic problem called the lockers problem. Here is...

    The Lockers Problem There is a well-known mathematical logic problem called the lockers problem. Here is a description of the problem 23 24 25 26 A new high school has just opened! There are 50 lockers in the school and they have been numbered from 1 through 50. When the school opens the first student to enter the school walks into the hallway and opens all the locker doors! Afterwards, the second student closes each door whose number is a...

  • Hi, I need help in solving Python programming problem below: Thank you. Best Regards When John...

    Hi, I need help in solving Python programming problem below: Thank you. Best Regards When John gambles at the casino, he always uses a special system of tactics that he devised himself. It's based on always betting in one of two ways in each game: • betting exactly one chip (to test his luck); • betting all-in (he bets everything he has). Wins in the casino are paid equal to the wager, so if he bets C chips and wins,...

  • I need help making my debits and credits equal to $20,743. Amount Name Bive Raiderndventure park...

    I need help making my debits and credits equal to $20,743. Amount Name Bive Raiderndventure park ndusted Tool Bouane DORT 8810 Laansa capital credit casni MONIS OCCOLTO Interest payadio $7000 10,000 Equipiant 420 360 2,100 Vrtinc pens 250 Aunis pavadi Insurance expanso 1605 Wagas expenso 380 Uncamca ratno CC HVITY Covinces 5650 mawing expenses SUPPS M. Tannskuwrindrawan 100 can Ticket verenver Accumulated Depreciation Sign Accumulated Depreciation Achustyc Jacumulated Depuccion-Equipment 495 Prepaid Insurance 100 Thierest expenso Depreciation expenso NOTES Rivaba Blue...

  • can someone help with the blue reader project, please? I have the journal entries I need...

    can someone help with the blue reader project, please? I have the journal entries I need help with journal ledger and trial balance so I can I do the financial statements. thanks can someone help me the ledger and trial balance please, I posted all the information about the picture Credit The accounting cycle illustrated below is designed to provide information about a company's profitability for lack thereof) along with many other important financial characteristics. This same accounting Cycle is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT