Some time ago, Chengdu 9-year-old students error Correction Olympiad title This article in the online burst red.
This article does not pay attention to the news of the Prodigy, only from the computer implementation point of view to verify the correctness of the problem.
Topic description (Requirement description):
150 light lights, each with a pull-wire switch control, sequentially numbered 1,2,3,...,150. Pull each of the lights with a multiple of number 3, then pull each of the lights with a multiple of 5, and then light the number of lights after pulling.
Use brute force method to get the right answer:
For the sake of simplicity, we can reduce the 150 lamps by 10 times times to 15 lamps, which makes it easy to mark the final result of the lamp with brute force-exhaustive methods:
650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M00/8C/83/wKioL1hu6YOwAt4rAASRuE1sWo4527.jpg-wh_500x0-wm_ 3-wmp_4-s_1870775940.jpg "title=" iterator.jpg "alt=" Wkiol1hu6yowat4raasrue1swo4527.jpg-wh_50 "/>
Answer:
The first time to pull out the multiples of 3, 15÷3=5 a
The second time to pull out the multiples of 5, 15÷5=3 a
Focus!!!
The problem is on the second pass, the second time really to pull out 3 lights, no!!!
Calculate 3 and 5 common multiple have 15÷ (3x5) = 1, that is, the second time, there are 1 (15th) lamp is the first time, the second pull is not again light?
So the second time actually pulled out 2 lights (5th lights, 10th lights), while the 1 lights (15th lamp), then the last how many lights?
15-5-2+1=9 Lamp
By 10 times times the above reduction, violence is poor, you will find the right answer!
Then expand 10 and know that 90 is the right answer!
This topic, the purpose is to examine common multiple related knowledge, just a few laps.
Refining key points (demand analysis)
150 lights, numbered in order 1-150, initially in the light state, each lamp has two state changes: Light/Dark (switch variable).
From the above sentence, we can refine a data structure light, with two key attributes, see the following code:
struct light{int index;//First property, index indicates the number of the current lamp int isOn; The second property, ISON, indicates whether the current light is lit//constructor light () {index = 0; At initialization time, the number is 0, and the following code sets exactly which number isOn = 1; According to test instructions, when initializing, all lights are in the light state, so set to 1, indicating that the light is on, if the ISON is 0, indicating that the lamp is dark}};
Pull the cable of the lamp with a multiple of number 3 and pull each of the lights with a multiples of 5.
1) in computer code, how do we represent the concept of division of integers?
Very simple, with the C language of the% symbol, the symbol represents a modulo operation (or call the remainder operation).
For example: 15%5 = (15-3*5) = 0 means that the remainder is 0, then 15 is divisible by 5, and 15 is divisible by 5. 15%4 = (15-3*4) =3 means that the remainder is 3, and there is no concept of division and being positive in the case of surplus numbers
So, by using computer code to represent division, we can use conditional statements to take a modulo action to represent:
int x = 15; int is a c keyword that represents an integer (including natural and negative numbers) if ((x% 5) = = 0)//If the result of x%5 is 0{//then the x can be divisible by 5//And the result is processed}else {//No cannot be divisible by 5//You can handle the case that cannot be divisible by 5. You can also not process//If you do not handle the else state, then you can else{} This code is not written, as if nothing happened}
2) in the computer code, how do we indicate the switching state?
Two different ways:
Conditional statement, cumbersome int onoff = 1; 0 means off, 1 means open, initialize, onoff = 1 means open//marka:if (onoff = = 1)//If OnOff ==1, the description is open in case {onoff = 0;//turn him off}else//Otherwise onoff = 0, description is off case {onoff = 1;//open him up}
You will find that the use of conditional judgments is cumbersome and not efficient. At this point we can introduce a classic operator of C, ^ (XOR or manipulation)
Since the status indicated by the switch operation is 0 (off)/1 (on), it is easy to use XOR to represent the switch
int onoff = 1; 0 means off, 1 means open, initialize, onoff = 1 means open onoff ^= 1; Indicates that if onoff=1, then becomes 0, if onoff=0, then becomes 1//as long as the sentence, the equivalent of the above Marka all the conditional judgment code, you will find, really less write a lot of code, and quite aesthetic
With these basics in place, it's easy to write code that validates through a computer program.
Code implementation:
#define lightcount 150int main () { //Allocates 150 lights of memory, when initialized, ISON is true, indicating that the light is lit light lights[lightcount]; //numbered 150 lights, index = [ 1-150] for (int i = 0; i < lightcount; i++) { lights[i].index = i+1; } //traverse 150 lights, which can be divisible by 3 (% modulo operation =0), the tag variable is xor (x^1 means that if x is 0, it becomes 1, If X is 1, it becomes 0, the classic switch variable) for (int i = 0; i < lightcount; i++) { if (lights [I].index) % 3 == 0) { lights[i].ison ^= 1; //because the initialization is on, So the lamp, which can be divisible by 3, becomes off state   } } //traverse 150 lights, which can be divisible by 5 (% modulo operation =0), The tag variable is XOR (x^1 indicates that if X is 0, it becomes 1, if X is 1, then 0, the classic switch variable) for (int i = 0; i < lightcount; i++) { if ((lights[i].index) % 5 == 0) { lights[i].isOn ^= 1; } } //to the above code, General Inon = 1, indicating that the light is on, whether it is closed, we will output the counter to see how many lights are lit int count = 0; The //counter is initialized to 0 //to traverse 150 lights, the counter adds 1 if ison = 1, and prints the index number of the illuminated lamp and the ISON status (at this point, Affirmation ison = 1) for (int i = 0; i < lightcount; i++) { if (Lights[i].ison) { count++; if (count % 3 == 0) printf ("Index number of the illuminated lamp:%03d Status:%d\n ", lights[i].index, lights[i].ison); else printf ("Illuminated lamp index number:%03d status:%d ", lights[i].index, lights[i]. ISON); } } // Finally print out how many lights are still on printf ("How many lights are there?? answer is:%d\n", count); GetChar (); return 0;}
Run the code:
650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M01/8C/86/wKiom1hu6euQwUh4AAJ2jQiieUI484.jpg-wh_500x0-wm_ 3-wmp_4-s_2028213622.jpg "title=" light150.jpg "alt=" Wkiom1hu6euqwuh4aaj2jqiieui484.jpg-wh_50 "/>
We will be 150 lamps:
#define LIGHTCOUNT 150
Change to 15 lamp:
#define LIGHTCOUNT 15
Run the code:
650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/8C/86/wKiom1hu6iHRBxIFAAANWg2NkQo588.png-wh_500x0-wm_ 3-wmp_4-s_2857409720.png "title=" Light15.png "alt=" Wkiom1hu6ihrbxifaaanwg2nkqo588.png-wh_50 "/>
In this way, we have verified the problem in C language.
In this simple code, most of the basic core elements of the C language program are involved:
struct defines the data structure light and two attributes (member variables)
In any programming language, there are three types of statements that can be used to solve any programmatic problem: for---> Looping statements if---> conditional statements other than the statement, called the sequential statement of the Heavenly Circle, does not out of these three statements. Billions of lines of operating system, dozens of lines of simple program, are composed of these three statements.
Thus, C language is a simple, powerful language, especially suitable for students of the third year and above to study
By:
1, such as Struct/for/if/int 32 fixed keywords 2, 3 large statements (circular statements, conditional statements and sequential statements) 3, such as +-*/% > = = < ^ ... Dozens of operators
Combination.
C language is definitely a good tool for pioneering thinking.
When thinking exercises on paper, it's called Olympic Math!
When the mind is connected with a computer, it is called the Information Olympics!
Took the Austrian Number Cup good position, the small rise first has the bonus points
Take the information Olympic, Tsinghua University directly Rob, finally became Microsoft, Google, Facebook dishes.
This article from "with the wind and the green shirt minded Xianfeng line" blog, reprint please contact the author!
From the 150 lights, the number of the Olympic math.