java枚举蓝桥杯_[蓝桥杯][历届试题]回文数字 (Java代码)暴力枚举

本文介绍了一种通过纯暴力方式解决特定数学问题的算法。该算法遍历六位数范围内的所有可能,筛选出前后对称且各位数字之和等于给定目标值的整数。代码采用Java编写,并考虑了不同位数的处理方法。

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

解题思路:纯暴力,简单理解

注意事项:

参考代码:import java.util.Scanner;

public class Main

{

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

int a, b, c, d, e, f;

int flag = 0;

for (int i = 10000; i <= 999999; i++)

{

int s=0;

if (i <= 99999)

{

int b1 = i / 10000;

int c1 = i / 1000 % 10;

int d1 = i / 100 % 10;

int e1 = i / 10 % 10;

int f1 = i % 10;

if (b1 == f1 && c1 == e1)

{

s+=b1+c1+d1+e1+f1;

}

}

else

{

a = i / 100000;

b = i / 10000 % 10;

c = i / 1000 % 10;

d = i / 100 % 10;

e = i / 10 % 10;

f = i % 10;

if (a == f && b == e && c == d)

{

s+=a+b+c+d+e+f;

}

}

if (s==n)

{

System.out.println(i);

flag=1;

}

}

if (flag == 0)

{

System.out.println("-1");

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值