Posts

Showing posts from May, 2016

uva problem 10963 - The Swallowing Ground - solution

# include < iostream > using namespace std ; int main ( ) { int t , w ; cin > > t ; while ( t - - ) { cin > > w ; int y1 , y2 , y [ w ] , flag = 1 ; for ( int i = 0 ; i < w ; i + + ) { cin > > y1 > > y2 ; y [ i ] = y1 - y2 ; if ( y [ 0 ] ! = y [ i ] ) flag = 0 ; } if ( flag ) cout < < " yes " < < endl ; else cout < < " no " < < endl ; if ( t ) cout < < endl ; } return 0 ; }

uva 10004 - Bicoloring -solution

# include < cstdio > # include < queue > # include < vector > using namespace std ; vector < int > v [ 200 ] ; int bfs ( int node ) { int color [ 200 ] = { 0 } ; int visit [ 200 ] = { 0 } ; queue < int > q ; q . push ( node ) ; color [ node ] = 1 ; while ( ! q . empty ( ) ) { int f = q . front ( ) ; q . pop ( ) ; int d = color [ f ] = = 1 ? 2 : 1 ; int l = v [ f ] . size ( ) ; for ( int i = 0 ; i < l ; i + + ) { node = v [ f ] [ i ] ; if ( color [ f ] = = color [ node ] ) return 0 ; if ( ! visit [ node ] ) { q . push ( node ) ; color [ node ] = d ; visit [ node ] = 1 ; } } } return 1 ; } int main ( ) { int n , l , a , b ; while ( 1 ) { scanf ( " %d " , & n ) ; ...

UVA problem 706 - LC-Display - solution

# include < cstdio > # include < string.h > using namespace std ; int main ( ) { int s ; char n [ 20 ] , ch ; while ( 1 ) { scanf ( " %d %s " , & s , n ) ; if ( s = = 0 & & n [ 0 ] = = '0' ) break ; int l = strlen ( n ) ; for ( int i = 0 ; i < l ; i + + ) { if ( i ) printf ( " " ) ; printf ( " " ) ; if ( n [ i ] = = '1' | | n [ i ] = = '4' ) ch = ' ' ; else ch = '-' ; for ( int j = 0 ; j < s ; j + + ) printf ( " %c " , ch ) ; printf ( " " ) ; } printf ( " \n " ) ; for ( int k = 0 ; k < s ; k + + ) { for ( int i = 0 ; i < l ; i + + ) { if ( i ) printf ( " " ) ; if ( n ...

UVA 392 : Polynomial Showdown - Solution

# include < cstdio > using namespace std ; int main ( ) { int cof [ 9 ] ; while ( 1 ) { for ( int i = 8 ; i > - 1 ; i - - ) { if ( scanf ( " %d " , & cof [ i ] ) ! = 1 ) return 0 ; } int flag = 1 ; for ( int i = 8 ; i > 0 ; i - - ) { if ( cof [ i ] ! = 0 ) { if ( flag ) { if ( cof [ i ] = = 1 ) printf ( " x " ) ; else if ( cof [ i ] > 1 ) printf ( " %d x " , cof [ i ] , i ) ; if ( cof [ i ] = = - 1 ) printf ( " -x " ) ; else if ( cof [ i ] < - 1 ) printf ( " - %d x " , - 1 * cof [ i ] ) ; if ( i ! = 1 ) printf ( " ^ %d " , i ) ; flag = 0 ; } ...

UVA 661 - Blowing Fuses : Solution

# include < iostream > using namespace std ; int main ( ) { int n , m , capacity , seq = 1 ; while ( cin > > n > > m > > capacity ) { if ( n = = 0 & & m = = 0 & & capacity = = 0 ) return 0 ; int swis [ 21 ] = { 0 } ; int c [ 21 ] ; for ( int i = 1 ; i < = n ; i + + ) cin > > c [ i ] ; int max = 0 , sum = 0 ; for ( int i = 0 ; i < m ; i + + ) { int t ; cin > > t ; if ( swis [ t ] = = 0 ) { sum + = c [ t ] ; swis [ t ] = 1 ; } else { sum - = c [ t ] ; swis [ t ] = 0 ; } if ( sum > max ) max = sum ; } cout < < " Sequence " < < seq + + < < end...

UVA 621 Secret Research - solution

# include < iostream > # include < string > using namespace std ; int main ( ) { int n ; string s ; cin > > n ; while ( n - - ) { cin > > s ; if ( s = = " 1 " | | s = = " 4 " | | s = = " 78 " ) cout < < " + " < < endl ; else if ( s [ s . length ( ) - 1 ] = = '5' & & s [ s . length ( ) - 2 ] = = '3' ) cout < < " - " < < endl ; else if ( s [ s . length ( ) - 1 ] = = '4' & & s [ 0 ] = = '9' ) cout < < " * " < < endl ; else if ( s [ 0 ] = = '1' & & s [ 1 ] = = '9' & & s [ 2 ] = = '0' ) cout < < " ? " < < endl ; } return 0 ; }

UVA 696 : How Many Knights -Solution

# include < cstdio > using namespace std ; int main ( ) { int m , n ; while ( scanf ( " %d %d " , & m , & n ) ! = EOF ) { if ( m = = 0 & & n = = 0 ) return 0 ; int ans , r , c ; r = m ; c = n ; if ( m > n ) { int t = m ; m = n ; n = t ; } if ( m = = 1 ) ans = n ; else if ( m = = 2 ) ans = n / 4 * 4 + ( n % 4 = = 1 ) * 2 + ( n % 4 > 1 ) * 4 ; else ans = ( m * n + 1 ) / 2 ; printf ( " %d knights may be placed on a %d row %d column board. \n " , ans , r , c ) ; } return 0 ; }

UVA 10196 -Check the Check : Solution

# include < cstdio > # include < string.h > using namespace std ; char board [ 8 ] [ 9 ] ; int check ( int wi , int wj , int bi , int bj ) { int i , j ; i = wi ; j = wj ; while ( - - j > = 0 ) { if ( board [ i ] [ j ] = = '.' ) continue ; if ( board [ i ] [ j ] = = 'q' | | board [ i ] [ j ] = = 'r' ) return 1 ; else break ; } j = wj ; while ( + + j < 8 ) { if ( board [ i ] [ j ] = = '.' ) continue ; if ( board [ i ] [ j ] = = 'q' | | board [ i ] [ j ] = = 'r' ) return 1 ; else break ; } j = wj ; while ( - - i > = 0 ) { if ( board [ i ] [ j ] = = '.' ) continue ; if ( board [ i ] [ j ] = = 'q' | | board [ i ] [ j ] = = 'r' ) return 1 ; else break ; } i = wi ; while ( + + i < 8 ) { ...