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
what are the advantages of sap on other software ?
What is the first message line that any language learning prints on the screen? and why?
i want to improve my english vocab for racking campus written exam plz tell me best book for prepration......
what is the meaning of without standing arrears?
I am looking for NIC Sample papers or any patern of questions/ syllabus plz, send me on hamid.khan135@yahoo.in Regard
What r the attributes using in Win Runner?
Difference between views and index in sas programming
what will we require to build project with the help of oracle
what is the filters in biztakk server? where it can use?
Difference of Console, web & windows applications?
4. What is the need of START 0? Instead if can we use any other numeric? If we use what will happen?
In OB52 , How to define two open posting period, Like only 5 and 8 posting should be open.. should not open 6 and 7..period..
what are other resources are needed for this mantis?
Is anyone has done the ASP.NET MVC4 workshop course(2 days) from Peers Technologies. Let me know. I need to talk before joining ASP.NET MVC4 training at peers, Hyderabad.
write a sql qwery which include joining of two tables 4 marks mainframe