Photolog

Through the Looking-Glass
2010-10-12: Through the Looking-Glass
My radio speaks is binary!
2010-10-10: My radio speaks is binary!
Gigaminx: (present for my birthday)
2010-09-16: Gigaminx: (present for my birthday)
Trini on bike
2010-09-05: Trini on bike
Valporquero
2010-08-28: Valporquero
My new bike!
2010-08-22: My new bike!
Mario and Ana's wedding
2010-08-13: Mario and Ana's wedding
Canyoning in Guara
2010-08-07: Canyoning in Guara
Trini and Mari in Marbella
2010-08-05: Trini and Mari in Marbella
Trini and Chelo in Tabarca
2010-08-03: Trini and Chelo in Tabarca
Valid XHTML 1.1
Log in
Back to list of problems

Traffic Lights

161.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int num;
int cycles[100];

int green(int i, int j)
{
	i %= (2*cycles[j]);
	if ((i>=0) && (i<cycles[j]-5)) {
		return 1;
	} else {
		return 0;
	}
}

void calculate(void)
{
	int start=cycles[0]-5;
	int i,j;

	for(i=0; i<num; i++) {
		if ((cycles[i]-5)<start) {
			start=cycles[i]-5;
		}
	}
	for(i=start; i<=5*60*60; i++) {
		int ok=1;
		for(j=0; j<num; j++) {
			if (!green(i,j)) {
				ok=0;
				break;
			}
		}
		if (ok) {
			printf("%02d:%02d:%02d\n", i/3600, (i%3600)/60, i%60);
			return;
		}
	}
	printf("Signals fail to synchronise in 5 hours\n");
}

int main(int argc, char *argv[])
{
	int a,b,c;

	while(1) {
		while(fscanf(stdin, " %d %d %d", &a, &b, &c)!=3) {
			exit(1);
		}
		num=2;
		cycles[0]=a;
		cycles[1]=b;
		if ((a==0) && (b==0) && (c==0)) {
			exit(0);
		} else while(c!=0) {
			cycles[num++]=c;
			if (fscanf(stdin, " %d", &c)!=1) {
				exit(1);
			}
		}
		calculate();
	}
	exit(0);
}