Java打印出所有的水仙花数的实现代码
作者:mdxy-dxy 时间:2023-03-06 17:24:22
题目:打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。
程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
程序设计:
public class exp2{
public static void main(String args[]){
int i=0;
math mymath = new math();
for(i=100;i<=999;i++)
if(mymath.shuixianhua(i)==true)
System.out.println(i);
}
}
class math
{
public int f(int x)
{
if(x==1 || x==2)
return 1;
else
return f(x-1)+f(x-2);
}
public boolean iszhishu(int x)
{
for(int i=2;i<=x/2;i++)
if (x % 2==0 )
return false;
return true;
}
public boolean shuixianhua(int x)
{
int i=0,j=0,k=0;
i=x / 100;
j=(x % 100) /10;
k=x % 10;
if(x==i*i*i+j*j*j+k*k*k)
return true;
else
return false;
}
}
更多内容请大家关注脚本之家之前发布的文章。
标签:java,水仙花
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java超详细讲解设计模式之一的工厂模式
2023-03-26 21:08:04
![](https://img.aspxhome.com/file/2023/6/101766_0s.png)
android开发教程之startActivityForResult使用方法
2022-04-03 13:46:16
nacos使用占位符${}进行参数配置的方法
2022-07-15 08:59:07
![](https://img.aspxhome.com/file/2023/4/63334_0s.png)
JavaWeb实现文件上传下载功能实例详解
2023-05-08 19:43:51
![](https://img.aspxhome.com/file/2023/3/62633_0s.jpg)
Java实现的双向匹配分词算法示例
2023-06-23 08:29:32
![](https://img.aspxhome.com/file/2023/2/70522_0s.jpg)
android实现简单音乐播放器
2021-06-28 22:07:26
![](https://img.aspxhome.com/file/2023/2/138032_0s.gif)
Android实现字母雨的效果
2023-12-13 02:26:33
![](https://img.aspxhome.com/file/2023/7/139037_0s.gif)
Springboot内置的工具类之CollectionUtils示例讲解
2023-08-23 20:47:13
![](https://img.aspxhome.com/file/2023/0/58440_0s.jpg)
Java源码解析之详解ImmutableMap
2023-11-23 08:06:07
![](https://img.aspxhome.com/file/2023/3/59223_0s.png)
java实现抽奖功能解析
2021-08-29 16:08:21
javascript checkbox全选和反选的简单实现
2022-08-31 14:16:32
C#中Html.RenderPartial与Html.RenderAction的区别分析
2022-03-17 01:27:59
C语言实现2D赛车游戏的示例代码
2022-04-03 05:57:33
![](https://img.aspxhome.com/file/2023/4/117794_0s.jpg)
JavaWeb开发基于ssm的校园服务系统(实例详解)
2022-11-07 16:40:48
![](https://img.aspxhome.com/file/2023/6/63646_0s.png)
Android开发使用Messenger及Handler进行通信的方法示例
2022-02-23 23:35:22
Android圆形头像拍照后“无法加载此图片”的问题解决方法(适配Android7.0)
2023-11-18 23:26:56
mybatis一直加载xml,找到错误的解决方案
2022-08-12 14:55:26
解决IDEA无法下载maven依赖的问题
2023-12-05 17:06:59
![](https://img.aspxhome.com/file/2023/3/88503_0s.png)
Android仿QQ空间动态界面分享功能
2023-11-13 11:03:58
![](https://img.aspxhome.com/file/2023/9/138399_0s.gif)
Jenkins+Maven+SVN自动化部署java项目
2021-06-01 11:28:27
![](https://img.aspxhome.com/file/2023/7/61837_0s.png)