简单JAVA programming 帮个忙!!

来源:百度知道 编辑:UC知道 时间:2024/06/23 01:20:51
Write a program that will find all the three digit natural numbers that are equal to the sum of the cubes of their digits. Maximum 3 digit number is 999. What is the first? (Hava to be able to find all the possibilities)

THANKXX!!!!!!!!!

求水仙花数?
20道经典Java编程题目之一.

是说三位整数.等于它百位、十位、个位上各个数的三次方的和

public class Flower {
/**打印出所有的"水仙花数",
* 所谓"水仙花数"是指一个三位数,
* 其各位数字立方和等于该数本身**/
public static void main(String[] args) {
for (int i = 101; i <= 999; i++) {
String str = i + "";
if (Math.pow((i / 100), 3)
+ Math.pow(Integer.parseInt((str.substring(1, 2))), 3)
+ Math.pow(Integer.parseInt((str.substring(2, 3))), 3) == i) {
System.out.println(i);
}
}
}
}

public class PrcNum {

public PrcNum() {
}

public static void main(String[] args) {
for(int i = 100; i <= 999; i ++) {
if(PrcNum.right(i)) {
System.out.println(i);
}
}
}

public static boolean right(int i) {
int res = 0;
String stri = i + "";
if(!stri.matches("\\d{3}")) {
retu