[Zhengqing] [1897] 985's Traffic Light Challenge

Problem Description

985的红绿灯难题
Time limit: 1 SecMemory limit: 128 MB

Description

Given that the red, green and yellow lights each last r, g and y seconds, 985 finds that the light has just turned green, and he wants to know what color the light will be after x seconds.

Input

The first line contains an integer t, the number of test cases.
Each test case contains four integers x, g, y, r.
Note: 1 <= t <= 30, 1 <= x, g, r, y <= 1e9.

Output

If the light turns red after x seconds output R, if it turns green output G, otherwise output Y.

Sample Input

1
2
3
4
3
5 5 2 8
7 5 2 8
16 5 2 8

Sample Output

1
2
3
Y
R
G

HINT

Traffic light order: green->yellow->red->green

Problem Analysis

A trivial sign-in problem
Just take x%(r+y+g) and work it out
Note that r+y+g can overflow int

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h> 
int main() { 
    int T; 
    scanf("%d",&T); 
    while(T--) { 
        long long x,r,g,y; 
        scanf("%lld %lld %lld %lld",&x,&g,&y,&r); 
        x%=r+g+y; 
        if(x<g) 
            printf("G\n"); 
        else { 
            x-=g; 
            if(x<y) 
                printf("Y\n"); 
            else
                printf("R\n"); 
        } 
    } 
    return 0; 
}

Problem link:ZZULI 1897 985 Traffic Light Problem

Loading...