http://acm.pku.edu.cn/JudgeOnline/problem?id=1211

Код:
#include <stdio.h>
struct light{
        int type[2] ;
        /* 0->red , 1->green */
}light[100] ;
int min_red ;
void make_light( int red_time , int tail )
{
        light[tail].type[0] = red_time ;
        light[tail].type[1] = red_time-5 ;
        if( !tail ) min_red = red_time ;
        else
                if( min_red > red_time ) min_red = red_time ;
}
int IsGreen( int time , int which )
{
        if( time >= 2*light[which].type[0]/* cycle */ )
                time %= 2*light[which].type[0] ;
        if( time < light[which].type[1] ) return 1 ;
        return 0 ;
}
void print_time( int second )
{
        int hour , minute ;
        minute = second / 60 ;
        second %= 60 ;
        hour = minute / 60 ;
        minute %= 60 ;
        printf( "%02d:%02d:%02d\n" , hour , minute , second ) ;
}
void count( int tail )
{
        int i , j , yes , end ;
        for( i=min_red-5 ; ; i++ )
                if( i <= 18000 ){ /* 60*60*5 */
                        for( yes=1 , j=0 ; j<tail ; j++ )
                                if( !IsGreen( i , j ) ){
                                        yes = 0 ;
                                        break ;
                                }
                        if( yes ){
                                        end = i ;
                                        print_time( end ) ;
                                        break ;
                        }
                }
                else{
                        puts( "Signals fail to synchronise in 5 hours" ) ;
                        break ;
                }

}
int main( void ){
        int red_time , tail , end=0 ;
        while( !end )
                for( tail=0 ; ; tail++ ){
                        scanf( "%d" , &red_time ) ;
                        if( !red_time )
                                if( tail ){
                                        count( tail ) ;
                                        break ;
                                }
                                else{
                                        end = 1 ;
                                        break ;
                                }
                        else make_light( red_time , tail ) ;
                }
}