当前位置:网站首页>for循环:水仙花案例
for循环:水仙花案例
2022-07-25 09:27:00 【anerl】
水仙花案例:
需求:在控制台输出所有的“水仙花数
问题描述
什么是“水仙花数”?
水仙花数是一个三位数水仙花数的个位、十位、百位的数字立方和等于原数如123 :13+23+33=1+8+27=36±123不是水仙花数如371 :33+73+13=27+343+1=371=371是水仙花数
分析(用371示例)
三位数的个位数字如何求:
371 :1就是原始数字对10进行取余运算的结果371%10=1
三位数的百位数字如何求:
371 :3就是原始数字除以100的结果(整除)371/100=3
三位数的十位数字如何求:
371 :371通过除以10,可以将7移动到个位上(整数) 371/10=3737通过对10进行取余运算可以得到最后一位的值7 37%10=7总结:371/ 10% 10 = 7
思考:任意数字的指定位上的数值如何求
先使用整除操作将要求的数字移动到个位上,再使用取余操作取出最后一位上的值123456789,先整除10000得到12345,再对10取余得到5
代码:
public class forTest03 {
public static void main(String[] args) {
//输出所有的水仙花数:遍历所有的三位数
for (int i =100;i<1000;i++){
//在计算之前获取三位数中每位上的值
int ge = i%10;
int shi = i/10%10;
int bai = i/10/10%10;
//判定条件是将三位数中每个位上的值取出来,计算立方和后与原始数字比较是否相等
if (ge*ge*ge + shi*shi*shi +bai*bai*bai ==i){
//输出满足条件的数字就是水仙花
System.out.println(i);
}
}
}
}
综合分析:
1.先建立循环
2.取出三位数中的个位十位百位数字
3.计算是否满足条件,满足条件输出
边栏推荐
- UE4 框架介绍
- 链表相关(设计链表及环链表问题)
- [recommended collection] with these learning methods, I joined the world's top 500 - the "fantastic skills and extravagance" in the Internet age
- SQL 题目整理
- [necessary for growth] Why do I recommend you to write a blog? May you be what you want to be in years to come.
- 测试基本概念
- 将 conda 虚拟环境 env 加入 jupyter kernel
- bug要素
- ES6 detailed explanation
- nodejs版本升级或切换的常用方式
猜你喜欢

Common methods of nodejs version upgrade or switching

Use of dictionary tree

Angr(八)——angr_ctf

关于slf4j log4j log4j2的jar包配合使用的那些事

message from server: “Host ‘xxx.xxx.xxx.xxx‘ is not allowed to connect to this MySQL server“

Angr(七)——angr_ctf

【专栏】RPC系列(理论)-夜的第一章

message from server: “Host ‘xxx.xxx.xxx.xxx‘ is not allowed to connect to this MySQL server“

数据库MySQL详解

CentOS install redis
随机推荐
Angr(九)——angr_ctf
复现 SSL_Anti-spoofing, 使用 wav2vec 2.0 和数据增强的自动说话人认证的欺骗攻击与深度伪造检测
数论--约数研究
异常处理Exception
SSM整合(简单的图书管理系统来整合SSM)
构建 Dompteur 容器问题小记
数组静态初始化,遍历,最值
The first week of the fifth stage
Exception handling exception
静态路由的配置(以华为eNSP为例)
Leetcode 560 前缀和+哈希表
Reflection 反射
An ASP code that can return to the previous page and automatically refresh the page
JDBC总结
[nearly 10000 words dry goods] don't let your resume don't match your talent -- teach you to make the most suitable resume by hand
21. Merge Two Sorted Lists
UE4源码的获取和编译
Snake games
SSM integration (simple library management system to integrate SSM)
关于slf4j log4j log4j2的jar包配合使用的那些事