TopCoder SquareDigits

ExpandedBlockStart.gif SquareDigits
 1  #include  < set >
 2  using   namespace  std;
 3 
 4  class  SquareDigits
 5  {
 6       private :
 7           int  s( int  x);
 8 
 9       public :
10           int  smallestResult( int  n);
11  };
12 
13  int  SquareDigits::s( int  x)
14  {
15       int  count  =   0 ;
16       while (x  >=   10 )
17      {
18           int  a  =  x  %   10 ;
19          count  +=  a  *  a;
20          x  =  x  /   10 ;
21      }
22 
23       if (x  <   10 )
24      {
25          count  +=  x  *  x;
26      }
27 
28       return  count;
29  }
30 
31  int  SquareDigits::smallestResult( int  n)
32  {
33       set < int >  se;
34 
35       for ( int  i = 0 ; ;i ++ )
36      {
37           int  k  =  i;
38          se.clear();
39           while ( 1 )
40          {
41              k  =  s(k);
42               if (se.find(k)  !=  se.end())
43              {
44                   break ;
45              }
46              se.insert(k);
47               if (k  ==  n)
48              {
49                   break ;
50              }
51          }
52 
53           if (k  ==  n  &&  se.find(k)  !=  se.end())
54          {
55               return  i;
56          } 
57      }
58  }

 

转载于:https://www.cnblogs.com/zhtf2014/archive/2010/10/19/1855882.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值