这听起来像是家庭作业,是的(是其他人),我问我的一个朋友,他正在学习C#借给我一些课堂练习来掌握它.
正如标题所说:我怎样才能检查一个数字是否是回文数?
我不是要求源代码(虽然它非常有用),而是有人解释了代码应该如何工作,以便它可以应用于许多不同的语言.
解决方案:
@statikfx在此搜索了SO并找到了解决方案.
n = num;
while (num > 0)
{
dig = num % 10;
rev = rev * 10 + dig;
num = num / 10;
}
// If (n == rev) then num is a palindrome
Run Code Online (Sandbox Code Playgroud)
Jed*_*ith 27
我通过将整数转换为字符串,然后反转字符串,然后比较相等来检查回文.这对您来说是最好的方法,因为您刚刚开始.
既然你在C#工作,这是家庭作业,我将使用非常模糊的Python,它不会帮助你:
def is_palindrome(i):
s = str(i)
return s[::-1] == s
Run Code Online (Sandbox Code Playgroud)
将其转换为C#,您将得到答案.
sch*_*der 13
大意:
Input number: 12321
Splitting the digits of the number, put them into an array
=> array [1, 2, 3, 2, 1]
Check if array[x] = array[arr_length - x] for all x = 0..arr_length / 2
If check passed => palindrome
Run Code Online (Sandbox Code Playgroud)