博客
关于我
杭电oj 2010 java实现
阅读量:485 次
发布时间:2019-03-07

本文共 1333 字,大约阅读时间需要 4 分钟。

以下是我关于查找特定范围内水仙花数的解决方案:

  • 问题理解

    水仙花数是一个三位数,其各位数字的立方和等于它本身。例如,153 = 1³ + 5³ + 3³ = 153。

  • 确定范围

    三位数i满足100 ≤ i ≤ 999。为了提高效率,可以将百位数设定在1到9之间,十位和个位设定在0到9之间。这减少了需要检查的数字数量。

  • 优化方法

    • 遍历每个三位数i,从100到999。
    • 拆分i的各位数字,分别计算它们的立方和。
    • 检查立方和是否等于i本身。如果是,则记录该数字。
  • 代码实现

    使用Java编写代码,以读取输入的起始值m和终止值n,遍历在m到n之间的所有整数i,检查其是否为水仙花数。

  • 处理细节

    • 确保正确地分解各位数字和计算立方和,避免计算错误。
    • 适当地处理输入过出的m和n是否在三位数范围内。
    • 如果找到的水仙花数为空,输出“no”;否则,逐个输出找到的数。
  • 测试与验证

    验证代码的正确性,可以手动测试几个已知的水仙花数,如153、370、371、407,确保正确识别。

  • 以下是实现代码:

    import java.util.Scanner;public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int m = sc.nextInt();        int n = sc.nextInt();        StringBuilder sb = new StringBuilder();        for (int i = m; i <= n; i++) {            int a = i / 100;            int b = (i / 10) % 10;            int c = i % 10;            int sum = a * a * a + b * b * b + c * c * c;            if (sum == i) {                if (sb.length() > 0) sb.append("\n");                sb.append(i);            }        }        if (sb.length() == 0) {            System.out.println("no");        } else {            System.out.print(sb.toString());        }    }}

    代码解释

    • 输入处理:从标准输入读取m和n,确定范围。
    • 遍历范围:从m到n遍历每个数字i。
    • 数字拆分:将i拆分为百位数a、十位数b和个位数c。
    • 立方和计算:计算各位数字的立方和,检查是否等于i。
    • 结果收集:将符合条件的水仙花数收集到一个字符串中,一个新线加隔离不同数字。
    • 输出结果:如果没有找到水仙花数则输出“no”,否则输出收集的数字。

    这个解决方案简洁高效,适用于查找三位数范围内的水仙花数。

    转载地址:http://dnjcz.baihongyu.com/

    你可能感兴趣的文章
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>