/lockers

Displaying lockers

Page 1 of 1

5 Replies - 8069 Views - Last Post: 06 February 2010 - 06:17 AM Rate Topic: -----

#1 mamaru   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-December 09

/lockers

Posted 31 January 2010 - 02:47 AM

A high school has 1000 students and 1000 lockers,one locker for each student,On the first day of school,the principal plays the following game: She asks the first student to open all the lockers. She then ask the second student to close all the even numbered lockers, The thrd student is asked to check every third locker . if it is open ,the student closes it.; if it is closed,the student will open it. The fourth student is ask to check every fourth locker, same thing like the third student. The remaining student will continue the game. In general, the nth student checks every nth locker. After all the students have taken their turns, 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 the lockers and the locker numbers that are open. Test your program for the ff. inputs,1000,5000.
i don't understand the real problem..


public class Locker
{
private static boolean locker[] = new boolean[1001];
public static void main(String[] args)
{
int n, v, s, opened=0, closed=0;
boolean temp;
//person 1 opens all lockers:
n = 1;
while (n<=1000)
{
locker[n] = true;
n++;
}
//person s reverses every s(th) locker:
v = 2;
for(n=1;n<1000;n++)
{
s = n+1; v =s;
while (h<=1000)
{
temp = !locker[h];
locker[s] = temp;
s += v;
}
}


System.out.println("Opened: " );
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: /lockers

#2 Qball522   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 86
  • Joined: 06-October 08

Re: /lockers

Posted 31 January 2010 - 10:26 AM

Fist I might suggest a better naming convention for your variables.
s,h and v, do not give much insight as to what they are for.
Was This Post Helpful? 0
  • +
  • -

#3 MKunstman   User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 194
  • Joined: 26-October 09

Re: /lockers

Posted 31 January 2010 - 10:31 AM

I might also include that you should initialize to 1000...as right now you have 1001. 1001 would give you lockers from 0-1000 which is 1001 lockers not 1000.
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,223
  • Joined: 15-July 08

Re: /lockers

Posted 31 January 2010 - 10:36 AM

Like Qball522 said, use better naming attributes...Here is some pseudocode for you to look at:
boolean[] lockers = new boolean[1000]; // 1000 lockers

set all lockers to false

numberOfTimes = input....
for int i = 1; i < numberOfTimes; i += i; // If 2, ever 2nd, if 3, ever 3rd...
	if  lockers at index i is open
		close
	else 
		 open

print all lockers



See, the logic is in the step variable (the third one in the for loop). If you tell it to step by the current index, then the 1st student will change the state of every locker (to true), then the 2nd will change every 2nd, and the 3rd, every 3rd locker, all the way to n students. Good Luck
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,223
  • Joined: 15-July 08

Re: /lockers

Posted 31 January 2010 - 08:03 PM

Also, I have to hank EdwinNameless's blog post. He goes and takes the liberty of actually writing a tutorial on it:

http://www.dreaminco...?showentry=2107

Nice Job and cheers!
Was This Post Helpful? 0
  • +
  • -

#6 Guest_mamaru*


Reputation:

Re: /lockers

Posted 06 February 2010 - 06:17 AM

It only shows the end of the game.. Lockers that are open and close at the end.. It doesn't show the whole part of the game..

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 the lockers and the locker numbers that are open.

package Locker1;

/**
 *
 * @author Merz
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {


        System.out.print("Open lockers are :");
        for( int i =2;i<=1000;i++)
{
      for( int j =2;j<= i;j++)
      {
   if( (i%j) == 0)
   {
       if( i == (j*j))
       {
   System.out.print("  "+ i);


       }

   }

      }


        }
System.out.println("");

       System.out.println("Closed lockers are : ");

        for( int i =1;i<=1000;i++)
        {
      for( int j =3;j<= i;j++)
      {
   if( (i%j) != 0)
   {

   System.out.print("  "+ i);

   }


   }

      }


        }

    }

Was This Post Helpful? 0

Page 1 of 1