24h購物| | PChome| 登入
2012-07-16 15:42:45| 人氣1,579| 回應0 | 上一篇 | 下一篇

[UVA] 161 - Traffic Lights

推薦 0 收藏 0 轉貼0 訂閱站台


 Traffic Lights 

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

00:00:40
00:05:00

題目真的有點不好理解, 黃燈固定 5 秒, 紅燈的時間 = 綠燈+黃燈,
最後給你 t = 0 時, 是第一次全燈的時候, 當這一次的綠燈結束時(t = 週期最小的-5),
然後去計算下一次的綠燈時間

#include <iostream>
#include <cstdio>
#include <sstream>
using namespace std;
int main() {
    while(1) {
        int A[105], n = 0, time[105] = {};
        while(scanf("%d", &A[n]) == 1) {
            if(A[n] == 0)
                break;
            n++;
        }
        if(A[0] == 0)
            break;
        int s = 0xffffff, i;
        for(i = 0; i < n; i++) {
            if(A[i] < s)
                s = A[i];
        }
        for(; s <= 18000; s++) {
            int yes = 1;
            for(i = 0; i < n; i++) {
                while(time[i]+A[i]-5 <= s) {
                    time[i] += 2*A[i];
                }
                if(!(time[i] <= s && s < time[i]+A[i]-5)) {
                    s = time[i]-1;
                    yes = 0;
                    break;
                }
            }
            if(yes)
                break;
        }
        if(s > 18000)
            puts("Signals fail to synchronise in 5 hours");
        else
            printf("%02d:%02d:%02d\n", s/3600, s%3600/60, s%60);
    }
    return 0;
}

台長: Morris
人氣(1,579) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][耐心] 403 - Postscript
此分類上一篇:[UVA] 630 - Anagrams (II)

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文