蓝桥官网练习系统-BASIC-009-特殊回文数

点击查看:蓝桥官网练习系统题解目录

题目009 - 特殊回文数

在这里插入图片描述

题解代码

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            F(sc.nextInt());
        }
        sc.close();
    }
     
    private static void F(int n){
        int count = 0;
         
        //5位或6位的截取一般都是3位数: 循环次数为 (29)*2 = 58
        //寻找5位的
        for(int a = 1; a <= 9; a++){
            for(int b = 0; b <= 9; b++){
                for(int c = 0; c <= 9; c++){
                    if(2*a + 2*b + c == n){
                        System.out.println("" + a + b + c + b + a);
                        count++;
                    }
                }
            }
        }
        //寻找6位的
        for(int a = 1; a <= 9; a++){
            for(int b = 0; b <= 9; b++){
                for(int c = 0; c <= 9; c++){
                    if(2*(a+b+c) == n){
                        System.out.println("" + a + b + c + c + b + a);
                        count++;
                    }
                }
            }
        }
        if(count == 0)
            System.out.println(-1);
    }
}
发布了52 篇原创文章 · 获赞 7 · 访问量 1811

猜你喜欢

转载自blog.csdn.net/weixin_44107920/article/details/103974152