import java.math.*;
public class Test {
public static void main(String[]args){
int firstP;//千位的数字
int secondP;//百位的数字
int thirdP;//个位的数字
System.out.println("100到999的水仙花数有:");
for(int i = 100; i < 999; i++)
{
firstP = i/100;
secondP = i%100/10;
thirdP = i%10;
if(Math.pow(firstP,3)+Math.pow(secondP, 3)+Math.pow(thirdP, 3)==i)
System.out.print(i+"\t");
}
}
}
有人回答了,我也晒一下自己的程序/**
* this program is to calculate all of the Narcissus
* @author yinjicheng
* time: 2010-12-29
*/
public class Narcissus
{
/**
* this method is check whether one number "i" is a Narcissus.
* @ i the number who use to check
* @ return return a boolean data .if it is a Narcissus ,then return true.otherwise it return false.
*/
public static boolean nar (int i)
{
boolean flag = false;
int a = i / 100;
int b = (i / 10) % 10;
int c = i % 10;
if (a*a*a + b*b*b + c*c*c == i)
{
flag = true;
}
return flag;
}
/**
* this is the main method,the program begin.
* @param args access a user prameter.
*/
public static void main(String[] args)
{
for (int i = 100; i < 1000; i ++ )
{
if (nar(i))
{
System.out.println(i);
}
}
}
}