uva 467 - Synching Signals(暴力+数学)

版权声明:本文为博主原创文章,未经博主同意不得转载。 https://blog.csdn.net/u011328934/article/details/36868691

题目连接:uva 467 - Synching Signals

题目大意:有n个红绿灯,给出红灯的时间t,那么该灯从0时刻開始就以2*t为周期绿黄红三灯交替。时间分别为t-5,5,t。问所这n个等从第一变为有一个灯不为绿灯開始,要多久才干变成全部的灯刚好都为绿灯。时间超过1小时输出unable to synch after one hour.

解题思路:一小时才3600秒,枚举秒数推断就可以。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int maxn = 1005;
const int maxd = 15;
const int INF = 0x3f3f3f3f;

char str[maxn];
int n, c[maxd];

inline bool isDight (char ch) {
    return ch >= '0' && ch <= '9';
}

void init () {
    n = 0;
    int num = 0, len = strlen(str);
    for (int i = 0; i <= len; i++) {
        if (isDight(str[i]))
            num = num * 10 + str[i] - '0';
        else {
            c[n++] = num;
            num = 0;
        }
    }
}

bool judge (int u) {
    for (int i = 0; i < n; i++) {
        int d = u % (2 * c[i]);

        if (d >= c[i] - 5)
            return false;
    }
    return true;
}

int main () {
    int cas = 1;
    while (gets(str) != NULL) {
        init();

        int s = INF;
        for (int i = 0; i < n; i++)
            s = min(s, c[i]);
        s -= 5;

        bool flag = true, t = true;
        for (int u = s+1; u <= 3600; u++) {

            if (t) {
                if (judge(u))
                    t = true;
                else
                    t = false;
            } else {
                if (judge(u)) {
                    flag = false;
                    printf("Set %d synchs again at %d minute(s) and %d second(s) after all turning green.\n", cas++, u/60, u%60);
                    break;
                }
            }
        }

        if (flag)
            printf("Set %d is unable to synch after one hour.\n", cas++);
    }
    return 0;
}

posted on 2019-05-12 16:41  xfgnongmin  阅读(142)  评论(0编辑  收藏  举报

导航