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