题解 | #回文对称数#

回文对称数

https://www.nowcoder.com/practice/5b143af8328f4e42adf5e10397ae44ef

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        //方法一
        for(int i = 1; i <= n; i++){
            int x = 0;
            int t = i;
            while(t != 0){
                x *= 10;
                x += t % 10;
                t /= 10;
            }
            if(i == x){
                System.out.println(i);
            }
        }
        //方法二
//         for(int i = 1; i <= n; i++){
//             StringBuilder str = new StringBuilder().append(i);
//             if(i == Integer.parseInt(str.reverse().toString())){
//                 System.out.println(i);
//             }
//         }
    }    
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务