d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java > Bathroom Stalls
Add Reply New Topic New Poll
Member
Posts: 41
Joined: Nov 11 2012
Gold: 0.00
Dec 5 2013 10:24am
It is a wall-researched fact that men in a restroom generally prefer to maximize their distance from already occupied stalls, by occupying the middle of longest seqeunce of un occupied places.
For example, consider the situation where ten stalls are empty.
_ _ _ _ _ _ _ _ _ _
The first visitor will occupy a middle position:
_ _ _ _ _ x _ _ _ _
The next visitor will be in the middle of the empty area at the left.
_ _ x _ _ x _ _ _ _
Write a program that reads the number of stalls and then prints out diagrams in the format given above when the stalls become filled, one at a time.
Hint: Use an array of boolean values to indicate whether a stall is occupied.

For the life of me I can't figure out any way to actually do this. all i could come up with was:

Code
import java.util.Arrays;
import java.util.Scanner;
public class Bathroom
{
public static void main(String[] args)
{
boolean[] stalls = new boolean[10];
int x = stalls.length;
boolean first = true; //toggle switch betweeen left and right of initial midpoint
int position;
int a;
position = x/2;
while(position>0 && position <stalls.length)
{
if(first == true) //for first initialization first is true
{
stalls[position] = true; //set position midpoint to true (stall occupied)
position = (position-1)/2; //move left 1 point divide by 2 to get mid point of the next biggest section.
first = false; //toggle switch false to use 2nd formula on re-entering the loop
}
if(first == false) //for 2nd initialization first is false
{
a = (((stalls.length)-(stalls.length/2))/2) + (stalls.length/2); //finds midpoint between middle and end of array
stalls[a] = true; //set position true (stall taken)
first = true; //toggle switch true to use 1st equation upon re-entering the loop
}
for(int i = 0; i < stalls.length; i++) // checks each slot in array for true / false, sets "_" for false and "X" for true.
{
if(stalls[i] == true)
{
System.out.print("X ");
}
else if(stalls[i] == false)
{
System.out.print("_ ");
}

}
}
}
}



Yea i know its stupid, was wondering if anyone could give me a better hint as to what the heck I need to do.
what i have prints this

_ _ _ _ _ X _ X _ _ _ _ X _ _ X _ X _

Been staring at this for hours (at least 6 [yea ik im stupid]) trying to figure this out but i cant get it. Even asked my father to help me yesterday and we spent 3 hours trying different algorithms that wouldnt work. He knows how to solve it but using a way that I haven't learned so I cant use it..

This post was edited by IpreferSoda on Dec 5 2013 10:51am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 5 2013 11:59am
essentially you just need to do a binary search. if the spot is empty, fill it.
Member
Posts: 14,871
Joined: Jan 23 2009
Gold: 1,251.88
Jan 4 2014 03:03am
:thumbsup:

This post was edited by OzzyOzzyOzzy on Jan 4 2014 03:04am
Member
Posts: 16,218
Joined: Sep 27 2009
Gold: 13.00
Jan 22 2014 03:47am
If you don't have resolve it, here is the algorithm solution:
Code
int min = 0;
int max = stalls.length - 1;

while (min <= max)
{
int mid = (max + min / 2);
stalls[mid] = true;
max = mid - 1;
}


just have to add your Sysout

This post was edited by Bremen on Jan 22 2014 03:49am
Member
Posts: 11,610
Joined: Oct 28 2008
Gold: 1,795.00
Jan 22 2014 07:08am
What you don't realize is that that's inefficient, offset the first by one and let one space in between and you can have 5 simultaneously peeing before having to go next to someone. Chances are people are leaving by then already and you can fill in or if it's that congested and going to happen anyway better to hold off as long as possible with this new method. I call it, common sense
Member
Posts: 16,218
Joined: Sep 27 2009
Gold: 13.00
Jan 22 2014 11:40am
Anyway, I don't really understand the purpose of its exercise. It's not particularly helpful to say "hey, all have to go on the left". What I gave work, I try it, its just the left part treatment of a binary search. Doing a real binary search will just be more intelligent than the current subject, and will also fill all the right part.

By the way, I didn't really understand what you wrote.

Of course, if its also contain the right part of the restrooms, the code I have written is not correct, you just have to add if condition and to treat the right case. Its just a simple binary search anyway, you have to adapt for thesubject (I think I have misunderstood, about just using the left side ... Its all the restrooms (it feels weird to me to use only one side), so you will just have to use a simple binary search).

This post was edited by Bremen on Jan 22 2014 11:44am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll