随便写了下public class Test { public Test() { this.t(); }
public void t(){ for (int i=100;i<=999;i++){ if (lifang(i/100)+lifang(i%100/10)+lifang(i%10%10)==i) System.out.println(i+" "); } } public int lifang(int n){ return n*n*n; } public static void main(String[] args) { new Test(); } }
这个题目考察的应该只是用最基本的东西,不用类方法做: public class Narcissistic { public static void main(String[] args) { int number, hundredsPlace, tensPlace, onesPlace; for (number = 100; number < 999; number++) { hundredsPlace = number / 100; tensPlace = number % 100 / 10; onesPlace = number % 10; if ( Math.pow(hundredsPlace, 3) + Math.pow(tensPlace, 3) + Math.pow(onesPlace, 3) == number) System.out.println(number); } } }
public Test() {
this.t();
}
public void t(){
for (int i=100;i<=999;i++){
if (lifang(i/100)+lifang(i%100/10)+lifang(i%10%10)==i) System.out.println(i+" ");
}
}
public int lifang(int n){
return n*n*n;
} public static void main(String[] args) {
new Test();
}
}
public class Narcissistic
{
public static void main(String[] args)
{
int number, hundredsPlace, tensPlace, onesPlace;
for (number = 100; number < 999; number++)
{
hundredsPlace = number / 100;
tensPlace = number % 100 / 10;
onesPlace = number % 10;
if ( Math.pow(hundredsPlace, 3) + Math.pow(tensPlace, 3) + Math.pow(onesPlace, 3) == number)
System.out.println(number);
}
}
}
谢谢这位大大,刚才俺把Math.pow()忘了,又加深了印象呵呵