我如何检查一个数字是否是回文? [英] How do I check if a number is a palindrome?

查看:36
本文介绍了我如何检查一个数字是否是回文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何判断一个数字是否是回文?

How do I check if a number is a palindrome?

任何语言.任何算法.(除了将数字变成字符串然后将字符串反转的算法).

Any language. Any algorithm. (except the algorithm of making the number a string and then reversing the string).

推荐答案

对于任何给定的数字:

n = num;
rev = 0;
while (num > 0)
{
    dig = num % 10;
    rev = rev * 10 + dig;
    num = num / 10;
}

如果 n == rev 那么 num 是一个回文:

If n == rev then num is a palindrome:

cout << "Number " << (n == rev ? "IS" : "IS NOT") << " a palindrome" << endl;

这篇关于我如何检查一个数字是否是回文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆