如何找出所有回文数回文

2023-09-11 22:36:58 作者:星语恋丹

一个回文数或数字回文是一种对称号像16461,剩下的当其位反转相同。

术语回文选自回文,其指的是字状转子,根据反转其字母不变衍生

第一个回文数(十进制)分别是:

 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,...
 

如何找出下面的所有回文数,也就是说,10000?

解决方案

生成所有回文到特定的限制。

 公共静态设置<整数GT; allPalindromic(INT限制){

    设置<整数GT;结果=新的HashSet<整数GT;();

    的for(int i = 0; I< = 9和;&安培; I< =限制;我++)
        result.add(ⅰ);

    布尔CONT = TRUE;
    的for(int i = 1;续;我++){
        StringBuffer的转=新的​​StringBuffer(+ I).reverse();
        CONT = FALSE;
        为(字符串D:,0,1,2,3,4,5,6,7,8,9.split(,)){
            INT N =的Integer.parseInt(+ I + D + REV);
            如果(N< =限制){
                CONT = TRUE;
                result.add(N);
            }
        }
    }

    返回结果;
}
 
东方博宜 1386 小丽找半个回文数

检验palindromicity

使用字符串

 公共静态布尔isPalindromic(字符串S,诠释我,诠释J){
    返回的J  -  I< 1 || s.charAt(ⅰ)== s.charAt(j)条和安培;&安培; isPalindromic(S,I + 1,J-1);
}

公共静态布尔isPalindromic(int i)以{
    字符串s =+我;
    返回isPalindromic(S,0,s.length() -  1);
}
 

使用整数

 公共静态布尔isPalindromic(int i)以{
    INT的len =(int)的Math.ceil(Math.log10第(i + 1));
    对于(INT N = 0; N< LEN / 2; N ++)
        如果((ⅰ/(int)的Math.pow(10,n))的10%!=
            (ⅰ/(int)的Math.pow(10,LEN  -  N  -  1))%10)
            返回false;
    返回true;
}
 

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 derived from palindrome, which refers to a word like rotor that remains unchanged under reversal of its letters.

The first 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, ...

How to find out all palindromic numbers below, say, 10000?

解决方案

Generating all palindromes up to a specific limit.

public static Set<Integer> allPalindromic(int limit) {

    Set<Integer> result = new HashSet<Integer>();

    for (int i = 0; i <= 9 && i <= limit; i++)
        result.add(i);

    boolean cont = true;
    for (int i = 1; cont; i++) {
        StringBuffer rev = new StringBuffer("" + i).reverse();
        cont = false;
        for (String d : ",0,1,2,3,4,5,6,7,8,9".split(",")) {
            int n = Integer.parseInt("" + i + d + rev);
            if (n <= limit) {
                cont = true;
                result.add(n);
            }
        }
    }

    return result;
}

Testing for palindromicity

Using Strings

public static boolean isPalindromic(String s, int i, int j) {
    return j - i < 1 || s.charAt(i) == s.charAt(j) && isPalindromic(s,i+1,j-1);
}

public static boolean isPalindromic(int i) {
    String s = "" + i;
    return isPalindromic(s, 0, s.length() - 1);
}

Using integers

public static boolean isPalindromic(int i) {
    int len = (int) Math.ceil(Math.log10(i+1));
    for (int n = 0; n < len / 2; n++)
        if ((i / (int) Math.pow(10, n)) % 10 !=
            (i / (int) Math.pow(10, len - n - 1)) % 10)
            return false;
    return true;
}