How do we go about solving this question? A little hint would be appreciated.
Vishal Patil
Level 5
What do I do here to solve this question?
Resolved
Comments (5)
- Popular
- New
- Old
You must be signed in to leave a comment
Roaayaa
19 November 2018, 13:09
Hi guys, what mean about t%5
0
Pavlo Plynko Java Developer at CodeGym
29 July 2018, 15:43
The full cycle is 3 + 1 + 1 = 5 minutes.
So, at first you should cut all full cycles from t.
You can do it like this: t % 5
Then you can analyze in which interval result is:
0 <= t < 3
3 <= t < 4
4 <= t < 5
+8
Vishal Patil
29 July 2018, 16:56
Yes. Thank you. Hope I would have thought about this problem like this earlier. Well I too did come up with another solution, but thank you for the tip. This is better than what I came up with.
In C we cannot use % operators on float data type variables right, I just searched that in Java we can.
+2
Arko Sarkar
25 August 2018, 11:13
I got the solution but I want to know more on how do you get at t % 5, can you explain the logic behind that?
Because t % 5 gives values 0, 1, 2, 3, 4.
And we want first 3 minutes red, 4th as green and 5th as yellow. How to adjust these 1-5 values in the t % 5 = 1-4 values. Need to understand logic behind it!
I did it like:
if(t>=0 && t<3)
System.out.println("green");
else if(t>=3 && t < 4)
System.out.println("yellow");
else
System.out.println("red");
0
Pavlo Plynko Java Developer at CodeGym
27 August 2018, 05:57
More specifically, for real number, t % 5 gives values 0 - 4,999999...
0