Ultraviolet A 467-Synching Signals
Question Link
Given several traffic lights, each traffic light time indicates a red light in time seconds, a green light in time-5 seconds, and a yellow light in 5 seconds.
After all the lights turn green, after a change, all the time required for the green light
Idea: as long as the time is counted as one hour, that is, 3600 seconds, the brute force is passed. Each time, the minimum time that can change the light is added, and the color status of each lamp is recorded, until all turns green
Code:
# Include
# Include
# Include using namespace std; char str [105]; int ti [15], s [15], r [15], n; int cas = 0; void solve () {int k = ti [0]; // s = 0 green, s = 1 yellow, s = 2 red memset (s, 0, sizeof (s )); memset (r, 0, sizeof (r); for (int I = 1; I <n; I ++) k = min (k, ti [I]); k-= 5; int t = k; for (int I = 0; I <n; I ++) {r [I] = (ti [I]-5) -k; if (r [I] = 0) {r [I] = 5; s [I] = 1;} else s [I] = 0 ;} while (t <= 3600) {k = r [0]; int sum = 0; (; Sum <n; sum ++) if (s [sum]) break; if (sum = n) break; for (int I = 1; I <n; I ++) k = min (k, r [I]); for (int I = 0; I <n; I ++) {r [I]-= k; if (r [I]) continue; if (s [I] = 0) {s [I] = 1; r [I] = 5 ;} else if (s [I] = 1) {s [I] = 2; r [I] = ti [I];} else {s [I] = 0; r [I] = ti [I]-5 ;}t + = k ;}if (t> 3600) printf ("Set % d is unable to synch after one hour. \ n ", ++ cas); else {int minu = t/60; int se C = t % 60; printf ("Set % d synchs again at % d minute (s) and % d second (s) after all turning green. \ n ", ++ cas, minu, sec) ;}int main () {while (gets (str )! = NULL) {int len = strlen (str); n = 0; str [len ++] = ''; int num = 0; for (int I = 0; I <len; I ++) {if (str [I]> = '0' & str [I] <= '9 ') {num = num * 10 + str [I]-'0'; continue;} ti [n ++] = num; num = 0;} solve ();} return 0 ;}