1. Description of the problem
There are n lights, numbered 1-n. The first man turned the light on, and the second man pressed
All switches numbered in multiples of 2, the third person presses the switch of multiples of 3, and so on,
A total of k individuals, asked the last which lights on?
Sample input:
3 ·
Sample output:
1 5 6 7
2. Implementing the Code
/** * */ PackageCom.sunny.www.interview;/*** N Lights, K personal light problem * Light class *@authorSunny*/ Public classLamp {/*** Status of the lamp (1: Open; 0: OFF)*/ Private intstatus; /*** Number of the lamp*/ Private intCode; //use constant variables to avoid hard-coded Public Static Final intStatus_is_open = 1;//Open State Public Static Final intStatus_is_close = 0;//off State /*** Constructor Function *@paramStatus Light State *@paramnumber of code lights*/ PublicLamp (intStatusintcode) { Super(); This. Status =status; This. Code =Code; } /*** operation by Light*/ Public voidPress () {//If the previous state is off (status=0), press the light operation to turn the status to On (Status=1) if(Lamp.status_is_close = = This. Status) { This. Status =Lamp.status_is_open; }Else if(Lamp.status_is_open = = This. Status) {//If the previous state is turned on (Status=1), the light operation after the state turns off (status=0) This. Status =Lamp.status_is_close; } } Public intGetStatus () {returnstatus; } Public voidSetStatus (intstatus) { This. Status =status; } Public intGetCode () {returnCode; } Public voidSetcode (intcode) { This. Code =Code; }}/** * */ PackageCom.sunny.www.interview;ImportJava.util.HashMap;ImportJava.util.Map;ImportJava.util.Map.Entry;ImportJava.util.Scanner;/*** N lights, K personal lights problem * Light Operation class *@authorSunny*/ Public classlampoperation {/*** Number of lights*/ Private intN; /*** Number of people*/ Private intK; /*** information for storing n lamps (number of key= lights; example of value= lamp)*/ PrivateMap<integer,lamp>lamps; PublicLampoperation (intNintk) {Super(); This. N =N; This. K =K; } /*** Initialize the information of n lights*/ Public voidinitlamps () {Lamps=NewHashmap<integer,lamp> ( This. N); for(inti = 1; I <= N; i++) {lamp lamp=NewLamp (0, I);//the status of the lamp is off when instantiated (status = 0)Lamps.put (i, lamp);//Example Deposit Map } } /*** N Lights, K personal light operation*/ Public voidPressbatch () {//Traverse K person for(inti = 1; I <= This. k; i++){ //Traverse N Lights for(intj = 1; J <= This. N; J + +){ //take the surplus =0 if(j% i = = 0) {Lamps.get (j). Press (); } } } } /*** Print out the number of lights with the status open*/ Public voidPrintopenlamps () {System.out.println ("Status is the number of the lights that are turned on:"); //before iterating through an instance of a collection class, you must first determine whether it is empty if(NULL!= This. Getlamps () &&! This. Getlamps (). IsEmpty ()) { //Traverse Map for(Entry<integer, lamp> Entry: This. Getlamps (). EntrySet ()) {Lamp lamp=Entry.getvalue (); if(Lamp.status_is_open = =Lamp.getstatus ()) {System.out.print (Lamp.getcode ()+ " "); } } } } Public intGetn () {returnN; } Public voidSETN (intN) { This. N =N; } Public intgetk () {returnK; } Public voidSETK (intk) { This. K =K; } PublicMap<integer, lamp>Getlamps () {returnlamps; } Public voidSetlamps (Map<integer, lamp>lamps) { This. Lamps =lamps; } Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in);//receiving parameters from the console intn = sc.nextint ();//Number of Lights intK = Sc.nextint ();//Number of peopleLampoperation lampoperation =Newlampoperation (n, k); Lampoperation.initlamps (); //instantiation of N LightsLampoperation.pressbatch ();//operation by LightLampoperation.printopenlamps ();//Print the number of the lamp with the status open }}3. Operation Effect
The status of the turn-on lamp number:1 5 6 7
n Lights, K personal light on the Java implementation