水仙花数

问题描述
  求出所有的“水仙花数”。所谓的“水仙花数”,是指一个3位数,其各位数字的立方和等于该数本身。
 
输入格式
  程序使用for循环遍历所有三位数整数,不需要手动输入
 
输出格式
  遇到水仙花数输出
 
样例输入
  无输入
 
样例输出
153
370
371
407

 

数据规模和约定
  输入数据中每一个数的范围。
  例:100~999
 
使用嵌套for循环直接判断即可求出规定范围内的所有水仙花数。
代码如下:
 1 import java.util.*;
 2 import java.lang.*;
 3 public class Main {
 4     public static void main(String[] args)throws Exception{
 5         Scanner sc=new Scanner(System.in);
 6         for(int i=1;i<=9;i++) {
 7             for(int j=0;j<=9;j++) {
 8                 for(int k=0;k<=9;k++) {
 9                     if((i*i*i+j*j*j+k*k*k)==(i*100+j*10+k)) {
10                         System.out.println(i+""+j+""+k);
11                     }
12                 }
13             }
14         }
15     }
16 }

 

posted @ 2021-06-11 16:35  熊猫耳朵  阅读(116)  评论(0编辑  收藏  举报