zoukankan      html  css  js  c++  java
  • 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 
    
  • 相关阅读:
    bzoj3280
    bzoj3876
    洛谷 p1625
    bzoj1407
    bzoj1227
    bzoj1477 && exgcd学习笔记
    bzoj1345
    c#程序的config文件问题
    思维角度的重要性
    python异步初步窥探
  • 原文地址:https://www.cnblogs.com/chaolong/p/3109703.html
Copyright © 2011-2022 走看看