Posts

Showing posts from April, 2016

UVA10163 : Last Digit -solution

# include < cstdio > # include < math.h > # include < string.h > using namespace std ; int main ( ) { int a [ 21 ] = { 4 , 1 , 5 , 2 , 8 , 3 , 9 , 2 , 8 , 7 , 7 , 8 , 4 , 7 , 3 , 8 , 4 , 1 , 5 , 4 , 4 } ; int a4 [ 5 ] = { 0 , 4 , 8 , 2 , 6 } ; char n [ 111 ] ; while ( scanf ( " %s " , n ) ! = EOF ) { int l = strlen ( n ) ; if ( l = = 1 & & n [ 0 ] = = '0' ) return 0 ; int d , r , ans ; if ( l = = 1 ) d = n [ 0 ] - 48 ; else if ( l = = 2 ) d = ( n [ 0 ] - 48 ) * 10 + n [ 1 ] - 48 ; else d = ( n [ l - 3 ] - 48 ) * 100 + ( n [ l - 2 ] - 48 ) * 10 + n [ l - 1 ] - 48 ; if ( d = = 0 ) { printf ( " 0 \n " ) ; continue ; } r = d / 20 ; d = d % 20 ; if ( d = = 0 ) r - - ; r = r % 5 ; ans = ( a [ d ] + a4 [ r ] ) % 10 ; printf ( " %d ...

UVA 944 : Happy numbers - solution

# include < bits/stdc++.h > using namespace std ; int happy [ 410 ] ; bool unhappy [ 410 ] ; int digit_add ( int n ) { int sum = 0 ; while ( n ) { int a = n % 10 ; sum + = a * a ; n / = 10 ; } return sum ; } int ishappy ( int n ) { bool visit [ 410 ] ; memset ( visit , false , 410 ) ; visit [ n ] = true ; int count = 0 ; while ( 1 ) { count + + ; n = digit_add ( n ) ; if ( n = = 1 ) return count ; if ( happy [ n ] ) return happy [ n ] + count ; if ( visit [ n ] ) return 0 ; if ( unhappy [ n ] ) return 0 ; visit [ n ] = true ; } } void preset ( ) { memset ( happy , 0 , 410 ) ; memset ( unhappy , false , 410 ) ; for ( int i = 1 ; i < 410 ; i + + ) { int temp = ishappy ( i ) ; if ( temp = = 0 ) for ( int j = i ; j < 410 ; j * = 10 ) ...

UVA 10515 - Powers Et Al -solution

# include < cstdio > # include < math.h > # include < string.h > using namespace std ; int main ( ) { char m [ 111 ] , n [ 111 ] ; while ( scanf ( " %s %s " , m , n ) ! = EOF ) { int lm = strlen ( m ) ; int ln = strlen ( n ) ; if ( lm = = 1 & & ln = = 1 & & m [ 0 ] = = '0' & & n [ 0 ] = = '0' ) return 0 ; if ( ln = = 1 & & n [ 0 ] = = '0' ) { printf ( " 1 \n " ) ; continue ; } int d = m [ lm - 1 ] - '0' ; //last digit of m int cycle ; if ( d = = 0 | | d = = 1 | | d = = 5 | | d = = 6 ) { printf ( " %d \n " , d ) ; continue ; } else if ( d = = 2 | | d = = 3 | | d = = 7 | | d = = 8 ) cycle = 4 ; else if ( d = = 4 | | d = = 9 ) cycle = 2 ; ...

UVA 350. - Pseudo-Random Numbers. -solution

# include < cstdio > using namespace std ; int main ( ) { int z , i , m , l , c = 1 ; while ( scanf ( " %d %d %d %d " , & z , & i , & m , & l ) = = 4 ) { if ( ! z & & ! i & & ! m & & ! l ) return 0 ; int count = 0 ; int L = ( z * l + i ) % m ; l = L ; do { L = ( z * L + i ) % m ; count + + ; } while ( L ! = l ) ; printf ( " Case %d : %d \n " , c + + , count ) ; } return 0 ; }

UVA 11879 : Multiple of 17 - solution

# include < cstdio > # include < string.h > using namespace std ; int main ( ) { char n [ 111 ] ; while ( scanf ( " %s " , n ) ! = EOF ) { int l = strlen ( n ) ; if ( l = = 1 & & n [ 0 ] = = '0' ) return 0 ; int r = 0 ; for ( int i = 0 ; i < l ; i + + ) { r = r * 10 + n [ i ] - '0' ; r = r % 17 ; } if ( r = = 0 ) printf ( " 1 \n " ) ; else printf ( " 0 \n " ) ; } return 0 ; }

UVA 10591: Happy Number -solution

# include < bits/stdc++.h > using namespace std ; int happy ( int n ) { int temp = n ; bool visit [ 10000 ] ; memset ( visit , false , 10000 ) ; while ( 1 ) { int temp2 = 0 ; while ( temp ) { int a = temp % 10 ; temp2 + = a * a ; temp / = 10 ; } if ( temp2 = = 1 ) return 1 ; if ( temp2 = = n ) return 0 ; if ( visit [ temp2 ] ) return 0 ; visit [ temp2 ] = true ; temp = temp2 ; } } int main ( ) { int t , c = 1 ; scanf ( " %d " , & t ) ; while ( t - - ) { int n ; scanf ( " %d " , & n ) ; if ( happy ( n ) ) printf ( " Case # %d : %d is a Happy number. \n " , c + + , n ) ; else printf ( " Case # %d : %d is an Unhappy number. \n " , c + + , n ) ; } return 0 ; }

UVA 10235 - Simply Emirp : Solution

# include < bits/stdc++.h > using namespace std ; bool mark [ 1000000 ] ; void sieve ( ) { memset ( mark , true , 1000000 ) ; mark [ 1 ] = false ; for ( int i = 4 ; i < 1000000 ; i + = 2 ) mark [ i ] = false ; for ( int i = 3 ; i < 1000 ; i + = 2 ) if ( mark [ i ] ) for ( int j = i * i ; j < 1000000 ; j + = i * 2 ) mark [ j ] = false ; return ; } int main ( ) { int n ; sieve ( ) ; while ( scanf ( " %d " , & n ) = = 1 ) { int rev = 0 ; int temp = n ; while ( temp ) { rev = rev * 10 + temp % 10 ; temp / = 10 ; } if ( mark [ n ] ) { if ( mark [ rev ] & & n ! = rev ) printf ( " %d is emirp. \n " , n ) ; else printf ( " %d is prime. \n " , n ) ; } else printf ( " %d is no...

UVA 12503 : Robot Instructions -Solution

# include < bits/stdc++.h > using namespace std ; int main ( ) { int t ; scanf ( " %d " , & t ) ; while ( t - - ) { int n , a ; scanf ( " %d " , & n ) ; int j = 1 , pos = 0 ; int ins [ 105 ] = { 0 } ; char s [ 10 ] , b [ 5 ] ; for ( int i = 1 ; i < = n ; i + + ) { scanf ( " %s " , s ) ; if ( s [ 0 ] = = 'L' ) ins [ i ] = - 1 ; else if ( s [ 0 ] = = 'R' ) ins [ i ] = 1 ; else { scanf ( " %s %d " , b , & a ) ; ins [ i ] = ins [ a ] ; } pos + = ins [ i ] ; } printf ( " %d \n " , pos ) ; } return 0 ; }

UVA 11952: Arithmetic -Solution

# include < cstdio > # include < string.h > using namespace std ; char A [ 10 ] , B [ 10 ] , C [ 10 ] ; int one ( ) { int la = strlen ( A ) ; int lb = strlen ( B ) ; int lc = strlen ( C ) ; for ( int i = 0 ; i < la ; i + + ) if ( A [ i ] ! = '1' ) return 0 ; for ( int i = 0 ; i < lb ; i + + ) if ( B [ i ] ! = '1' ) return 0 ; for ( int i = 0 ; i < lc ; i + + ) if ( C [ i ] ! = '1' ) return 0 ; if ( la + lb ! = lc ) return 0 ; return 1 ; } int lowestbase ( ) { int base = 0 ; int la = strlen ( A ) ; int lb = strlen ( B ) ; int lc = strlen ( C ) ; for ( int i = 0 ; i < la ; i + + ) if ( base < A [ i ] - '0' ) base = A [ i ] - '0' ; for ( int i = 0 ; i < lb ; i + + ) if ( base < B [ i ] - '0' ) base = B [ i ] - '0' ; for ( int i = 0 ; i < lc ; i + + ) if ( base < C ...

UVA 11192 : Group Reverse -Solution

# include < cstdio > # include < string.h > using namespace std ; int main ( ) { int g ; while ( scanf ( " %d " , & g ) = = 1 ) { if ( g = = 0 ) break ; char s [ 110 ] ; getchar ( ) ; scanf ( " %s " , s ) ; int l = strlen ( s ) ; int nchar = l / g ; for ( int i = 1 ; i < = g ; i + + ) { int j = i * nchar ; int t = nchar ; while ( t - - ) printf ( " %c " , s [ - - j ] ) ; } printf ( " \n " ) ; } return 0 ; }

UVA 11455. : Behold my quadrangle -Solution

# include < cstdio > # include < algorithm > using namespace std ; int main ( ) { int t ; scanf ( " %d " , & t ) ; while ( t - - ) { unsigned long long int s [ 4 ] ; for ( int i = 0 ; i < 4 ; i + + ) scanf ( " %llu " , & s [ i ] ) ; sort ( s , s + 4 ) ; if ( s [ 0 ] = = s [ 1 ] & & s [ 1 ] = = s [ 2 ] & & s [ 2 ] = = s [ 3 ] ) printf ( " square \n " ) ; else if ( s [ 0 ] = = s [ 1 ] & & s [ 2 ] = = s [ 3 ] ) printf ( " rectangle \n " ) ; else if ( s [ 0 ] + s [ 1 ] + s [ 2 ] > = s [ 3 ] ) printf ( " quadrangle \n " ) ; else printf ( " banana \n " ) ; } return 0 ; }