酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

java 水仙花数的定义:一个三位数(100~999)的各位数的立方和等于它本身。例如,153是一个三位数的水仙花数,因为 1^3 + 5^3+ 3^3 =1 + 125 + 27=153 题目大意:输入一个数n,求100到整数n的区间内有多少个水仙花数m,并打印水仙花数的数量m,用空格分隔。若区间没有水仙花数,则输出not found. 输入描述: 输入一个整数n 输出描述: 水仙花数的数量m ,(找不到打印not found) 示例1:输入 :154 输出 : 1 示例2:输入 101 输出 :not found

以下是使用 Java 解决此问题的代码: ```java import java.util.Scanner; public class NarcissisticNumber { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int count = 0; for (int i = 100; i <= n && i <= 999; i++) { int hundreds = i / 100; int tens = (i % 100) / 10; int units = i % 10; if (hundreds * hundreds * hundreds + tens * tens * tens + units * units * units == i) { count++; } } if (count == 0) { System.out.println("not found"); } else { System.out.println(count); } } } ``` [2024-10-18 18:05:55 | AI写代码神器 | 209点数解答]

相关提问