码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-07-06 08:40:04      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   os   for   io   

题目连接: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;
}

uva 467 - Synching Signals(暴力+数学),布布扣,bubuko.com

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

标签:style   http   color   os   for   io   

原文地址:http://blog.csdn.net/keshuai19940722/article/details/36868691

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!