szabivan

HAPPY - v2: gets

Oct 21st, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int squares[10] = {0,1,4,9,16,25,36,49,64,81};
  4.  
  5. int f( int n ){
  6.   int result = 0;
  7.   while( n ) {
  8.     result += squares[ n%10 ];
  9.     n /= 10;
  10.   }
  11.   return result;
  12. }
  13.  
  14. int table[ 750 ];
  15.  
  16. int fillTable( int n ){
  17.   if( table[n] != 0 ) return table[n];
  18.   table[n] = -1;
  19.   int d;
  20.   if( (d = fillTable( f(n) ) ) != -1 ) d++;
  21.   return( table[n] = d );
  22. }
  23.  
  24. inline void initTable(){
  25.   for( int i = 0; i < 750; i++ ) table[ i ] = 0;
  26.   table[ 1 ] = 1;
  27.   for( int i = 2; i < 750; i++ ) fillTable( i );
  28. }
  29.  
  30. char line[ 32 ];
  31.  
  32. inline int getFn(){
  33.   char *p = line;
  34.   int result = 0;
  35.   while(*p) result += squares[ (*(p++))-'0' ];
  36.   return result;
  37. }
  38.  
  39. int main( int argc, char* argv[] ){
  40.   initTable();
  41.   int t,n;
  42.   scanf( "%d\n",&t);
  43.   while( t-- ){
  44.     gets( line );
  45.     printf("%d\n", table[ getFn() ] );
  46.   }
  47.   return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment