之前有寫過一個,剛好用得上,我前面也釋出過其他的程式設計練習內容,可以在我主頁檢視
/****3.水仙花數*****/
public static void main(String[] args){
for(int i=1;i<1000;i++){
if(isLotus(i))
System.out.print(i+" ");
}
System.out.println();
//判斷水仙花數
private static boolean isLotus(int lotus){
int m = 0;
int n = lotus;
int sum = 0;
m = n/100;
n -= m*100;
sum = m*m*m;
m = n/10;
n -= m*10;
sum += m*m*m + n*n*n;
if(sum==lotus)
return true;
else
return false;
之前有寫過一個,剛好用得上,我前面也釋出過其他的程式設計練習內容,可以在我主頁檢視
/****3.水仙花數*****/
public static void main(String[] args){
for(int i=1;i<1000;i++){
if(isLotus(i))
System.out.print(i+" ");
}
System.out.println();
}
//判斷水仙花數
private static boolean isLotus(int lotus){
int m = 0;
int n = lotus;
int sum = 0;
m = n/100;
n -= m*100;
sum = m*m*m;
m = n/10;
n -= m*10;
sum += m*m*m + n*n*n;
if(sum==lotus)
return true;
else
return false;
}