szabivan

HAPPY - v1

Oct 21st, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 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] != -1 ) return table[n];
  18.   table[n] = 0;
  19.   int d;
  20.   if( (d = fillTable( f(n) ) ) ) d++;
  21.   return( table[n] = d );
  22. }
  23.  
  24. inline void initTable(){
  25.   for( int i = 0; i < 750; i++ ) table[ i ] = -1;
  26.   table[ 1 ] = 1;
  27.   for( int i = 2; i < 750; i++ ) fillTable( i );
  28. }
  29.  
  30. int main( int argc, char* argv[] ){
  31.   initTable();
  32.   int t,n;
  33.   scanf( "%d",&t);
  34.   while( t-- ){
  35.     scanf("%d",&n);
  36.     printf("%d\n",table[ f(n) ]);
  37.   }
  38.   return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment