Advertisement
aryobarzan

RSH3

Jun 7th, 2011
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <sstream>
  4. using namespace std;
  5.  
  6. bool pal(int x)
  7. {
  8.     stringstream cnv;
  9.     string t;
  10.     cnv<<x;
  11.     cnv>>t;
  12.     string s=t;
  13.     reverse(&s[0],&s[0]+s.length());
  14.     return (s==t);
  15. }
  16.  
  17. int main()
  18. {
  19.     int ans=0;
  20.     for(int i=100000;i<1000000;++i)
  21.     {
  22.         if(i%(int)1e6==0)
  23.         {
  24.             cerr<<"R:"<<i<<" with:"<<ans<<endl;
  25.         }
  26.         int current=i;
  27.         while(current>=10)
  28.         {
  29.             if(pal(current))
  30.             {
  31.                 ans++;
  32.                 break;
  33.             }
  34.             int sum=0;
  35.             while(current)
  36.             {
  37.                 sum+=current%10;
  38.                 current/=10;
  39.             }
  40.             current=sum;
  41.         }
  42.     }
  43.     cout<<ans<<endl;
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement