# A bit fun of programming operations question

2008-06-04  来源：本站原创  分类：Java  人气：200

To see a very interesting programming problem: the hall, there are 64 lamps, each lamp is compiled numbers 1-64, respectively. Each lamp controlled by a switch. (Switch click, lights, and then click the lights turned off. Switch number and be controlled the same light.) Began when the lamp is Quanmie's. In accordance with the following rules are now pressed switches. The first time, all the lights lit. The second time, all the multiples of 2, the switch click. The third time, all the multiples of 3, the switch click. And so on. The first N times, all the multiples of N, the switch click. Question No. N times (N less than or equal 64) by End after the hall there is Jizhan Deng was lit. Tip can use long digital to do, very clever just fine, said a 64-bit, 0 or 1 on behalf of open or close down. Implementation and the original author wrote a basic idea is the same:

```/**
*
* @author: yanxuxin
* @date: 2010-3-13
*/
public class LightSwiter {

public static void main(String[] args) {
long lights = switchLights(3);
convert2Binary(lights);
System.out.println();
countOpenedLights(lights);
}

/**
*  Toggle switch
*
* @param sequence
* @return
*/
public static long switchLights(int sequence) {
long lights = 0l; // All are closed status, i.e.  64 Bits are  0
for (int i = 1; i <= sequence; i++) {
for (int j = 1; j <= 64; j++) {

if (j % i == 0) {  // Subject to the n number of times a multiple of
long temp = 1l;
temp = temp << (j - 1); // Ready to intercept the current bits

if ((lights & temp) == 0) { // Indicates that the current bits lights  0, That is, closed
lights = lights | temp; // Does not affect other digits to the left, change  lights The current position of the State is  1
}
else {
temp = ~temp;
lights = lights & temp; // Does not affect other digits to the left, change  lights The current position of the State is  0
}
}
}
}
return lights;
}

/**
*  Statistics on the number of lights
*
* @param lights
*/
public static void countOpenedLights(long lights) {
int remains = 0;
for (int k = 0; k < 64; k++) {
long temp = 1l;
temp = temp << k;
temp = (lights & temp); // Interception of current bit judgment
if (temp != 0) {
remains++;
System.out.println("The " + (k + 1) + " light is opened.");
}
}
System.out.println("Total " + remains + " lights are opened.");
}

/**
*  Translated into binary expressions show
*
* @param number
*/
public static void convert2Binary(long number) {
long res = number / 2;
long rem = number % 2;
if (res != 0) {
convert2Binary(res);
}
System.out.print(rem);
}
}
```

Topic answer trick is that the use of bit operations to transform the status of each one, very good idea.

• A bit fun of programming operations question 2008-06-04

To see a very interesting programming problem: the hall, there are 64 lamps, each lamp is compiled numbers 1-64, respectively. Each lamp controlled by a switch. (Switch click, lights, and then click the lights turned off. Switch number and be control

• JDK 7 in the functional programming ideas 2010-08-15

About JDK 7 there are too many exciting new features and exciting, especially Lambda Expressions! If you search in the search engines JDK 7, you will see a lot of discussion of the Lambda expression, it has been a controversial topic, which shows tha

• Longest common subsequence 2010-03-11

Longest common subsequence (Longest common subsequence, LCS), not with the longest common substring (Longest common substring), confused. In many cases, we want to know how similar the two strings, for example: two short sentences, or two DNA sequenc

• Post a dynamic language literacy 2010-11-30

From colleagues ppt ... ... 1, Why do we study the dynamic language? . Dynamic languages have become popular JavaScript Ruby on Rails Google Apps (Python) Python & Ruby on Android Add dynamic language features C # . Dynamic languages and static langu

• [Netease Wealth 10-year network programming competition warm-up] Third Question 2010-05-25

Feibo that all series can be expressed with the following formula: f (1) = 1 f (2) = 1 f (n) = f (n-1) + f (n-2) (n> = 3) Now we define the rules under which another series called "Xinbo that cut the number of columns", which is defined as: s

• [Netease Wealth 10-year network programming warm-up game] Third Question 2010-05-25

Feibo that all series can be expressed with the following formula: f (1) = 1 f (2) = 1 f (n) = f (n-1) + f (n-2) (n> = 3) Now we define the basis of this rule to another series called "Xinbo that all series," which is defined as: s (x) = 0 (x

• 3) Linux Programming Introduction - File Operations 2010-09-26

Files under Linux operating Introduction: In this section we will discuss the various file operations under linux function. File creation and read-write Each attribute Directory file operation Pipe File -----------------------------------------------

• EMC pen questions (the last a programming question) 2010-11-25

EMC yesterday to interview, there is a question I did not write it, just wrote a thought, want to subject under discussion is this: 7 * 8 a board that has 56 boxes. Put the ball on a random lattice. The ball can only be horizontal or vertical directi

• C Primer Plus 5 programming exercises in Chapter 8, Section 8, the answer to question their own 2010-12-23

Tossing the book for a long time this question, other issues are small, and soon have been written and is stuck in the last issue of line breaks. BS, first get the complete code: #include <stdio.h> #include <stdlib.h> #include <ctype.h>

• Database Programming in transactions on the question 2010-10-22

Into a problem recently, transactions in the database programming problem, we talk to see Problem Description: JDBC programming, such as a transaction, perform two different updates (Table 1, Table 2) data operation, the first update (Table 1) operat

• c language programming of the string operations 2010-02-25

1. // In the find and s series s1 Matching string, found by s2 The s and s1 Matching string replace 2. #include<stdio.h> 3. #include<string.h> 4. 5. void replace(char *s,char *s1,char *s2); 6. 7. int main(int argc,char *argv[]) 8. { 9. char s[

• Shanghai Huawei's a question on the pointer's programming 2010-04-30

I am from the network under a Huawei document under the Daquan questions in the subject: int A[nSize], Where a number of 0, the remaining non- 0 Integer, write a function int Func(int* A, int nSize), Make A put 0 Move to the back, a non- 0 Integer mo

• java programming source code file operations thought IO5 2010-05-05

package com.dirlist; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class BufferedInputFile { // Buffered input file public static String read(String filename) throws IOException{ //BufferedReader From ch

• java programming source code file operations thought IO6 2010-05-05

package com.dirlist; import java.io.BufferedReader; import java.io.IOException; import java.io.PrintWriter; import java.io.StringReader; public class FileOutputShortcut { /** * The shortcut text file output */ static String file="FileOutputShortcut.o

• java programming source code file operations thought IO8 2010-05-06

Explore channels and buffers package com.dirlist; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.io.*; public class ChannelCopy { private static final int BSIZE=1024; public static void main(String[] args) throws IOExce

• java programming source code file operations thought IO10 2010-05-08

package com.dirlist; import java.nio.*; public class IntBufferDemo { /** * View buffer, use view buffer encapsulates the byte buffer */ private static final int BSIZE=1024; public static void main(String[] args) { ByteBuffer bb=ByteBuffer.allocate(BS

• [Netease Wealth 10-year network programming competition warm-up] the second question 2010-05-25

Describe the calculation of a, b-th power modulo on the 9907 value. Enter the first line there is a positive integer T, T group of test data indicated. The next T lines, each line is a set of test data, consisting of two integers a and b. Where T <=

• NetEase Programming Contest 2010 qualifying first question 2010-05-30

Very depressed, the first inscribed an hour buttoned up, how to submit, but also submitted incorrect, strange ..... my own machine had been tested correctly, and estimate the output format is the format or what strange rules. ... It seems really is n

• [Netease Wealth 10-year programming Qualifying first] second question Wealth search box 2010-06-01

Description The search box in the proper way, when entering one or more characters, the search box will appear a certain number of prompt, as shown below: Now give your word and a number N a, please Shuchu tips Results , to simplify the problem, is p

• Java Programming ZIP compression and decompression operations 2010-07-15

http://www.360doc.com/content/06/0915/14/10610_208147.shtml

1
2
3
4
5
6
7
8
9
10