There is a room with 1000 light switches, numbered 1, 2, 3,
4, ... 1000, all turned off.
Outside the room there are 1000 men, numbered man1, man2,
...man 1000
In order, each man walks into the room and changes the
position of each switch that is a multiple of his number.
That is: man1 flips every switch
man2 flips switches 2, 4, 6, 8 ....1000
man3 flips switches 3, 6, 9, ..... 999
.....
Man 1000 flips switch 1000
After all 1000 men are done, how many switches are on?
Answer Posted / orenise
The answer is 31. here is the code...
private static void Q1() {
int firstMan = 1;
int lastMan = 1000;
int numOfSwitches = 1000;
boolean[] switches = new boolean[numOfSwitches + 1];
// Print the switches initial state.
System.out.println("switches");
int col = 0;
for (int j = 0; j < switches.length; j++) {
System.out.print("[" + switches[j] + "], ");
if (col >= 10) {
System.out.println();
col = 0;
}
col++;
}
for (int currentMan = firstMan; currentMan <= lastMan;
currentMan++) {
for (int switchNum = currentMan; switchNum <=
lastMan; switchNum++) {
int divids = switchNum % currentMan;
if (divids == 0) {
switches[switchNum] = !switches[switchNum];
}
}
}
// Print the switches final state.
System.out.println("switches");
int col2 = 0, sum = 0;
for (int j = 0; j < switches.length; j++) {
System.out.print("[" + switches[j] + "], ");
if (switches[j]) {
sum++;
}
if (col2 >= 10) {
System.out.println();
col2 = 0;
}
col2++;
}
System.out.println(" sum = " + sum);
}
| Is This Answer Correct ? | 3 Yes | 2 No |
Post New Answer View All Answers
Hey this is venkatesh.Please can any body tell me what is SFLNXTCHG?where we use this keyword?what perpuse we use this?Can you tell me in real time senario with example? And in 7 specification(RPG/400)what is the mandatory specification using programs?
what are all the ant command options
what is the difference between rename and label
Which language they use during interview?
Delta 5 weight scale not connect with oracle application what i can do?
What is the meaning of client-server application. The purpose of Client-Server Application. with description.
Tips for blog integration by www.esteemwebsolutions.com. Can Any body suggest me to how to make wonderful questions on web integration..
public static void main(String args[]) describe it
difference between mantis and other tools?
how do i add a column dynamically in a table by using java application?
Hello...has anyone interviewed with Information Management Services(IMS)located in Silver Spring MD??If yes,what can you share about the programming test and the analytical test?
I want sample papers for NIC Examination. Plz send them to my mail Id das.neelam@gmail.com . Plz send it today, tomorrow i've the exam.
Plz sent me in .net 2.0 interview Question & answers?
How to call dll API sub routine in VB Form.
how to study PHP my own? i reffered many sites.. but as being a fresher i couldnt follow those... which site will be the best one?