判断回文数

C小白之简单循环判断回文数


学号:16340085
[数据科学与计算机学院] (http://sdcs.sysu.edu.cn/)


目录


回文数的定义

A palindromic number 1 or numeral palindrome is a number that remains the same when its digits are reversed.
——from Wikipedia
译文( 一个回文数或数字回文是当它的数字被逆转时,一个保持不变的数字。)
e.g. 121 , 12321,8, 99 ………
刑如这些数字,将其各个数字前后逆转(即从右往左写),其数值不变,则该数是回文数。

Although palindromic numbers are most often considered in the decimal system, the concept of palindromicity can be applied to the natural numbers in any numeral system. Consider a number n > 0 in base b ≥ 2, where it is written in standard notation with k+1 digits ai as:

n=ki=0aibi
with, as usual, 0 ≤ ai < b for all i and ak ≠ 0. Then n is palindromic if and only if ai = ak −i for all i. Zero is written 0 in any base and is also palindromic by definition.

设计程序求回文数

 现在要设计一个简单程序:输入任意一个整数,判断是否为回文数,若是则输出Yes!,否则输出No!     
 先不管怎么执行判断,大致的思路如下:
  #include <stdio.h>  
  int main (void)     
  {  
     int i;
     scanf("%d",&i);
     judge (); //此处仅展示程序思路,具体的判断操作见下文
     printf();
     return 0;
  }

利用简单循环判断回文数

  鉴于目前学识尚浅,所接触了解的有关C的知识相当有限,就拿刚刚学到的循环的知识来解决这次的问题;在小标题2中声明简单的程序流程,具体代码如下:
#include <stdio.h>
int main (void)
{
   int num,mid,las=0;
   printf("请输入您需要判断是否为回文数的数字:\n");
   scanf("%d",&num);

   mid = num;
   while (mid)
  {
    las = las*10+mid%10;
    mid/=10;
   }

   if (las==num)
       printf("是!\n");
   else
       printf("否!\n");
   return 0;
}

试数检查程序

1. 键入代码

2.编译运行程序

这里写图片描述

3.试数

输入数字121,是回文数
输入数字8,是回文数
输入数字1234,不是回文数
……..
试数结果没有异常,该判断回文数程序完成;

简单解释判断操作

虽然一个整数是否为回文数单凭肉眼也能轻易判断,但是要让机器去判断,却反而没这么简单。
本次用了一个简单while 循环进行判断操作,具体的判断过程如下:
以输入数字1234为例:
1 . mid=1234 成立;
las=las*10+mid%10=4,mid=mid/10=123
2. mid=123 成立;
las=las*10+mid%10=3,mid=mid/10=12
3. mid=12 成立;
las=las*10+mid%10=2,mid=mid/10=1
4. mid=1 成立;
las=las*10+mid%10=1,mid=mid/10=0
5. m=0 不成立;
最终las的值为4321

再进入if else语句进行判断
las num
printf(“否\n”);


这就是一个判断回文数的简单程序。
(以上内容皆为个人观点,欢迎指点,感谢阅读)

[] 1 : A palindromic number or numeral palindrome is a number that remains the same when its digits are reversed. Like 16461, for example, it is “symmetrical”. The term palindromic is derived from palindrome, which refers to a word (such as rotor or “racecar” or even “Malayalam”) whose spelling is unchanged when its letters are reversed. The first 30 palindromic numbers (in decimal) are:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, … (sequence A002113 in the OEIS).
Palindromic numbers receive most attention in the realm of recreational mathematics. A typical problem asks for numbers that possess a certain property and are palindromic. For instance:

The palindromic primes are 2, 3, 5, 7, 11, 101, 131, 151, … (sequence A002385 in the OEIS).
The palindromic square numbers are 0, 1, 4, 9, 121, 484, 676, 10201, 12321, … (sequence A002779 in the OEIS).
Buckminster Fuller identified a set of numbers he called Scheherazade numbers, some of which have a palindromic symmetry of digit groups.

It is fairly straightforward to appreciate that in any base there are infinitely many palindromic numbers, since in any base the infinite sequence of numbers written (in that base) as 101, 1001, 10001, etc. (in which the nth number is a 1, followed by n zeros, followed by a 1) consists of palindromic numbers only.

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 16
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值