How to find out all palindromic numbers

后端 未结 8 1896
独厮守ぢ
独厮守ぢ 2020-12-14 12:05

A palindromic number or numeral palindrome is a \"symmetrical\" number like 16461, that remains the same when its digits are reversed.

The term palindromic is derive

8条回答
  •  孤街浪徒
    2020-12-14 12:34

    Brute force approach: Make a foreach loop from 1…10000 and test against the constraints. Even easier, convert the number to string, reverse it and compare it to the original value. This is inefficient and lame.

    Better approach: Think about the patterns of a palindrome. Think about the different possibilities there are for palindromes, depending on the length of the number. Now provide a method that generates palindromes of the given length. (I will not do this, because it's obviously homework.)

提交回复
热议问题