Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Java Programming Help?
A king once decided to grant amnesty to some of his prisoners due to lack of jail space rather than build a new prison. He instructed his jailer to carryout the following procedure to determine which prisoners were to be set free. The jailer was to first unlock each of the 1000 cells. Then starting with the 2nd cell (number 2), he turned the key in every 2nd cell. Then starting with the 3rd cell he turned the key in every 3rd cell; then starting the 4th, he turned the key in every 4th cell, etc. Each turn of the key either locked or unlocked the cell door. When he was completely done those with the unlocked doors could leave. Who were the lucky ones?
3 Answers
- 1 decade agoFavorite Answer
this sounds really easy.
You'll have a boolean for each prisoner (probably in an array) and then a series of for loops with if statements inside them that simply switch the booleans.
The first for would just be something like for(int i =1; i < 1000; i+2)
The second for would be something like for(int i=2; i < 1000; i+3)
and the last for would be like for(int i=3; i < 1000; i+4);
Source(s): Bunnies. - Mark aka jack573Lv 71 decade ago
Similar to what another answerer said, you would do this.
1. Create an array of 1000 holding booleans. Set them all to true. That is, they are locked.
2. Create a for loop that goes from 1 to 1000.
3. Create a second for loop inside this for loop that goes from the number of the first loop to 1000. that is, 1, then 2, then 3, etc. Make the increment the same as the start number.
4. Change the boolean type. From false to true, or true to false.
5. At the end, go through the array again and output which ones are false as they are opened.
for (int i = 1; i <= 1000; i++)
{
for (int j = i; j <= 1000; j = j + i)
{
// change the boolean here. Be aware that the index into the array should be j - 1 since an array starts at 0 not 1, and goes to 999 not 1000.
}
}
Hope that helps.
- SilentLv 71 decade ago
So what's your question?
We're not going to do your homework for you. If you're having trouble with a specific part of the assignment, ask a question and I'll be happy to help.