本文目錄一覽:
java面試題求解?
水仙花數只是自冪數的一種,嚴格來說三位數的3次冪數才成為水仙花數。
附:其他位數的自冪數名字
一位自冪數:獨身數
兩位自冪數:沒有
三位自冪數:水仙花數
四位自冪數:四葉玫瑰數
五位自冪數:五角星數
六位自冪數:六合數
七位自冪數:北斗七星數
八位自冪數:八仙數
九位自冪數:九九重陽數
十位自冪數:十全十美數
所以如果是求水仙花數的話只需要計算3位的,for循環解決,代碼我就不貼了,在網吧沒環境
java求8位的自冪數,哪裡出錯了?
因為Math.pow函數返回的值是double型,所以需要轉成int型的數據,把它轉換一下就行了。
完整的程序如下:
public class FF {
public static void main(String[] args) {
int a[]=new int [8];
int n=100000000;
for(int i=n/10;in;i++){
a[0]=i%10;
a[1]=(i-a[0])/10%10;
a[2]=(i-a[1]*10-a[0])/100%10;
a[3]=(i-a[2]*100-a[1]*10-a[0])/1000%10;
a[4]=(i-a[3]*1000-a[2]*100-a[1]*10-a[0])/10000%10;
a[5]=(i-a[4]*10000-a[3]*1000-a[2]*100-a[1]*10-a[0])/100000%10;
a[6]=(i-a[5]*100000-a[4]*10000-a[3]*1000-a[2]*100-a[1]*10-a[0])/1000000%10;
a[7]=(i-a[6]*1000000-a[5]*100000-a[4]*10000-a[3]*1000-a[2]*100-a[1]*10-a[0])/10000000%10;
if(i==(int)(Math.pow(a[7], 8))+
(int)(Math.pow(a[6], 8))+
(int)(Math.pow(a[5], 8))+
(int)(Math.pow(a[4], 8))+
(int)(Math.pow(a[3], 8))+
(int)(Math.pow(a[2], 8))+
(int)(Math.pow(a[1], 8))+
(int)(Math.pow(a[0], 8))){
System.out.println(i+” “);
}
}
}
}
運行結果:
24678050
24678051
88593477
用Java寫個關於「水仙花數」的程序?
按一下代碼執行:
public class woo {
public static void main(String args[]) {
System.out.println(“100-1000中的水仙花數有:”);
for(int i=100;i1000;i++){
int single = i%10;
int ten = i/10%10;
int hundred = i/10/10%10;
//水仙花數判斷要求
if(i == (single*single*single+ten*ten*ten+hundred*hundred*hundred)){
System.out.println(i);
}
}
}
}
擴展資料:
水仙花數只是自冪數的一種,嚴格來說3位數的3次冪數才稱為水仙花數。
一位自冪數:獨身數
兩位自冪數:沒有
三位自冪數:水仙花數
四位自冪數:四葉玫瑰數
五位自冪數:五角星數
六位自冪數:六合數
七位自冪數:北斗七星數
八位自冪數:八仙數
九位自冪數:九九重陽數
十位自冪數:十全十美數
參考資料:
水仙花數——百度百科
JAVA編程題 -求自冪數(題目如圖)
1000以內,先轉化char數組,迭代相加
public class ds {
public static void main(String[] args) {
ss();
}
public static void ss(){
for(int j =0;j1000;j++){
String str = j+””;
char [] a = str.toCharArray();
int count = 0;
for(int i=0;istr.length();i++){
int b = Integer.valueOf(a[i]+””);
count+=b*b*b;
}
if (count==j) {
System.out.println(j);
}
}
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/237420.html