特殊回文数 2021.12.04

该代码片段是一个Java程序,用于寻找所有五位和六位的回文数,其各位数字之和等于输入的正整数n。程序通过两个for循环分别遍历五位数和六位数,并检查每个数是否为回文(从左到右和从右到左读都相同),以及其各位数字之和是否等于n。如果满足条件,就打印出这个数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package introductory;

import java.util.Scanner;

/*
    123321是一个非常特殊的数,它从左边读和从右边读是一样的。
  输入一个正整数n, 编程求所有这样的五位和六位十进制数,满足各位数字之和等于n 。
 */
public class test05 {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.close();

        for(int m = 10000; m<=99999; m++){
            int a = m%10;
            int b = m/10%10;
            int c = m/100%10;
            int d = m/1000%10;
            int e = m/10000;
            if( a == e && b == d && a+b+c+d+e == n){
                System.out.println(m);
            }
        }

        for(int m = 100000; m<=999999; m++){
            int a = m%10;
            int b = m/10%10;
            int c = m/100%10;
            int d = m/1000%10;
            int e = m/10000%10;
            int f = m/100000;
            if( a == f && b == e && c == d && a+b+c+d+e+f == n){
                System.out.println(m);
            }
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值