Question:

Trying an experiment, each students walks into the building one at a time. ?

by  |  earlier

0 LIKES UnLike

The 1st student will open all 100 locker doors. The 2nd will close all the locker doors that have even numbers. The 3rd will change all the locker doors with numbers that are multiples of three (Change means closing locker door that are open and opening doors that are closed). The 4th student will do those with multiples of 4 and the 5th student will do multiples of 5, and so on. After 100 students have entered the school, which lockers will be open?

 Tags:

   Report

2 ANSWERS


  1. it's number 1..


  2. Please see

    http://answers.yahoo.com/question/index;...

    Just in case that link doesn't work, 361 lockers will remain open.

    I attacked the problem with a programming solution because, well, I'm a programmer. The following program not only calculates the proper number of open lockers, but also tells you the state of each locker

    Sorry about the lack of indenting. Y! strips out all attempts to indent properly.

    #define OPEN 1

    #define CLOSED 0

    int Lockers[1000];

    int nLocker;

    int nStudent;

    int nOpenCount;

    // Initialize all lockers to closed

    for (nLocker = 0; nLocker < 1000; nLocker++)

    { Lockers[nLocker] = CLOSED; }

    // Have the students reverse the state of the lockers

    for (nStudent = 1; nStudent <= 1000; nStudent++)

    {

    // Each student goes through all of the lockers

    for (nLocker = 1; nLocker <= 1000; nLocker++)

    {

    // Is this is a locker that the student is permitted to modify?

    if ( (nLocker % nStudent) == 0)

    {

    // If the locker is closed, open it, otherwise close it

    if (Lockers[nLocker-1] == CLOSED)

    {

    Lockers[nLocker-1] = OPEN;

    }

    else

    {

    Lockers[nLocker-1] = CLOSED;

    }

    }

    }

    }

    // Finally, count the open lockers

    nOpenCount = 0;

    for (nLocker = 0; nLocker < 1000; nLocker++)

    { if (Lockers[nLocker] == OPEN)

    {

    cout << "Locker " <<  nLocker+1 << " is OPEN" << \n;

    nOpenCount++;

    }

    else

    {

    cout << "Locker " <<  nLocker+1 << " is CLOSED" << \n;

    }

    }

    cout << "\nThe number of open lockers is " << nOpenCount << \n;

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions