题目连接: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(暴力+数学)
原文地址:http://blog.csdn.net/keshuai19940722/article/details/36868691