100c之28:回文数

问题

打印不超过n( n<256 )的,其平方具有对称性质的数( 也称回文数 )

分析

穷举即可, 如果这个数的平方很大超出了int的标识范围就必需采用 自守数 的计算方法。

解决方案

 1:  /**
 2:   * @file   028c.c
 3:   * @author Chaolong Zhang <emacsun@163.com>
 4:   * @date   Fri May 31 08:28:53 2013
 5:   * 
 6:   * @brief  打印不超过n( n<256 )的,其平方具有对称性质的数( 也称回文数 )
 7:   * 
 8:   */
 9:  
10:  
11:  #include <stdio.h>
12:  #include <math.h>
13:  
14:  int get_digits ( int  );
15:  
16:  
17:  int main(int argc, char *argv[])
18:  {
19:      int n;
20:      int m;
21:      int nn;
22:      char flag=1;
23:  
24:  
25:  
26:      for (n=0; n <= 256; ++n)
27:      {
28:          nn=n*n;
29:          flag=1;
30:          m=get_digits( nn );
31:          for (int i = 0; i < m; ++i)
32:          {
33:              flag = flag && ( ( nn/ ( int )pow( 10,i )%10 ) == ( nn/ ( int )pow( 10, m-i-1 )%10 )  );
34:              if (flag==1) continue;
35:              else break;
36:          }
37:          if (flag==1)
38:              printf (" the square of %d is %d \n",n, nn);
39:      }
40:      return 0;
41:  }
42:  
43:  
44:  int get_digits ( int number )
45:  {
46:      int i=1;
47:  
48:      while( ( number = number/ 10 ) > 0 ) i++;
49:      return i;
50:  }
51:  

输出结果

the square of 0 is 0 
the square of 1 is 1 
the square of 2 is 4 
the square of 3 is 9 
the square of 11 is 121 
the square of 22 is 484 
the square of 26 is 676 
the square of 101 is 10201 
the square of 111 is 12321 
the square of 121 is 14641 
the square of 202 is 40804 
the square of 212 is 44944 

转载于:https://www.cnblogs.com/chaolong/archive/2013/05/31/3109703.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值