UVA161 POJ1211 Traffic Lights【水题】

Traffic Lights
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 649 Accepted: 208

Description

One way of achieving a smooth and economical drive to work is to `catch’ every traffic light, that is have every signal change to green as you approach it. One day you notice as you come over the brow of a hill that every traffic light you can see has just changed to green and that therefore your chances of catching every signal is slight. As you wait at a red light you begin to wonder how long it will be before all the lights again show green, not necessarily all turn green, merely all show green simultaneously, even if it is only for a second.

Write a program that will determine whether this event occurs within a reasonable time. Time is measured from the instant when they all turned green simultaneously, although the initial portion while they are all still green is excluded from the reckoning.

Input

Input will consist of a series of scenarios. Data for each scenario will consist of a series of integers representing the cycle times of the traffic lights, possibly spread over many lines, with no line being longer than 100 characters. Each number represents the cycle time of a single signal. The cycle time is the time that traffic may move in one direction; note that the last 5 seconds of a green cycle is actually orange. Thus the number 25 means a signal that (for a particular direction) will spend 20 seconds green, 5 seconds orange and 25 seconds red. Cycle times will not be less than 10 seconds, nor more than 90 seconds. There will always be at least two signals in a scenario and never more than 100. Each scenario will be terminated by a zero (0). The file will be terminated by a line consisting of three zeroes (0 0 0).

Output

Output will consist of a series of lines, one for each scenario in the input. Each line will consist of the time in hours, minutes and seconds that it takes for all the signals to show green again after at least one of them changes to orange. Follow the format shown in the examples. Time is measured from the instant they all turn green simultaneously. If it takes more than five hours before they all show green simultaneously, the message ``Signals fail to synchronise in 5 hours’’ should be written instead.

Sample Input

19 20 0
30
25 35 0
0 0 0

Sample Output

扫描二维码关注公众号,回复: 10941040 查看本文章

00:00:40
00:05:00

Source
New Zealand 1993 Division I,uva 161

问题链接UVA161 POJ1211 Traffic Lights
问题简述:(略)
问题分析:看代码不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA161 POJ1211 Traffic Lights */

#include <iostream>
#include <map>
#include <cstdio>
#include <climits>

using namespace std;

const int N = 3600 * 5;

int main()
{
    int x;
    for(;;) {
        scanf("%d", &x);
        if(x == 0) break;

        map<int, int> cycles;
        map<int, int>::iterator iter;
        int cnt = 0, minx = 1e9 + 1;
        while(x) {
            cnt++;
            minx = min(minx, x);
            cycles[x] = 1;
            scanf("%d", &x);
        }

        if(cnt >= 2) {
            int i;
            for(i = minx - 5; i <= N; i++) {
                for(iter = cycles.begin(); iter != cycles.end(); iter++) {
                    int tmp = (i / (2 * iter->first)) * (2 * iter->first);
                    if(i >= tmp + iter->first - 5) break;
                }
                if(iter == cycles.end()) {
                    int h = i / 3600;
                    i %= 3600;
                    int m = i / 60;
                    int s = i % 60;
                    printf("%02d:%02d:%02d\n", h, m, s);
                    break;
                }
            }
            if(i > N)
                printf("Signals fail to synchronise in 5 hours\n");
        }
    }

    return 0;
}
发布了2289 篇原创文章 · 获赞 2373 · 访问量 265万+

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/105563128