"Para"-Trains
A helicopter drops two trains, each on parachute, onto a straight infinite railway line. There is an undefined distance between the two trains. Each faces the same direction, and upon landing, the parachute attached to each trains falls to the ground next to the train and detaches. Each train has a microchip that controls its motion. The chips are identical. There is no way for the trains to know where they are. You need to write the code in the chip to make the trains bump into each other. Each line of code takes a single clock cycle to execute.
You can use the follwing commands (and only these);
MF - moves the train forward
MB - moves the train backward
IF (P) - conditional that's satisfied if the train is next to a parachute. There is no "then" to this IF statement.
GOTO
Source: Microsoft Questions by Kiran Bondalapati
3 Comments:
MF
if(P) Goto Bump
MB
Bump
a1: MB GOTO a2;
a2: MF GOTO a3;
a3: MF if (P) GOTO a4;
GOTO a1;
a4: MF GOTO a4;
I've never really used GOTO statements before, but if we're allowed to use 'else' then it be like this I guess:
a1: MB GOTO a2;
a2: MF GOTO a3;
a3: MF if (P) GOTO a4; else GOTO a1;
a4: MF GOTO a4;
Am I right? ooo boy, I better be.
a1: MB GOTO a2;
a2: MF GOTO a3;
a3: MF if (P) GOTO a4;
GOTO a1;
a4: MF GOTO a4;
in the question its said that theres no then for the if. So, irrespective of whether if(p) is true or false, goto a1 is never reached. Hope ive understood the problem right
Post a Comment
<< Home