新聞中心
水仙花數(shù)java語(yǔ)言程序
求水仙花數(shù)的Java程序如下

創(chuàng)新互聯(lián)建站專注于日喀則企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站,商城網(wǎng)站開發(fā)。日喀則網(wǎng)站建設(shè)公司,為日喀則等地區(qū)提供建站服務(wù)。全流程按需設(shè)計(jì)網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)
public?class?E?{
public?static?void?main(String[]?args)?{
System.out.println("水仙花數(shù)");
for(int?i=100;i1000;i++){
int?a=i/100;
int?b=i/10%10;
int?c=i%10;
if(i==a*a*a+b*b*b+c*c*c){
System.out.print(i+"?");
}
}
}
}
運(yùn)行結(jié)果
水仙花數(shù)
153 370 371 407
java求水仙花數(shù)
按照你的要求編寫的求N位水仙花數(shù)的程序如下
import?java.util.Scanner;
public?class?CCC?{
public?static?void?main(String[]?args)?{
System.out.print("請(qǐng)輸入正整數(shù)N(3=N=7):");
Scanner?sc=new?Scanner(System.in);
int?N=sc.nextInt();
if(N3?||?N7){
System.out.println("N3?或者?N7");
return;
}
int?min=(int)Math.pow(10,?N-1);
int?max=(int)Math.pow(10,?N);
for(int?i=min;imax;i++){
int?tmp=i;
int?j=0;
int?a[]=new?int[N];
while(tmp!=0){
a[j]=tmp%10;
tmp=tmp/10;
j++;
}
int?sum=0;
for(int?k=0;kN;k++){
sum=sum+(int)Math.pow(a[k],N);
}
if(sum==i){
System.out.println(i);
}
}
}
}
運(yùn)行結(jié)果
請(qǐng)輸入正整數(shù)N(3=N=7):4
1634
8208
9474
用Java寫個(gè)關(guān)于“水仙花數(shù)”的程序?
按一下代碼執(zhí)行:
public class woo {
public static void main(String args[]) {
System.out.println("100-1000中的水仙花數(shù)有:");
for(int i=100;i1000;i++){
int single? = i%10;
int ten = i/10%10;
int hundred = i/10/10%10;
//水仙花數(shù)判斷要求
if(i == (single*single*single+ten*ten*ten+hundred*hundred*hundred)){
System.out.println(i);
}
}
}
}
擴(kuò)展資料:
水仙花數(shù)只是自冪數(shù)的一種,嚴(yán)格來說3位數(shù)的3次冪數(shù)才稱為水仙花數(shù)。
一位自冪數(shù):獨(dú)身數(shù)
兩位自冪數(shù):沒有
三位自冪數(shù):水仙花數(shù)
四位自冪數(shù):四葉玫瑰數(shù)
五位自冪數(shù):五角星數(shù)
六位自冪數(shù):六合數(shù)
七位自冪數(shù):北斗七星數(shù)
八位自冪數(shù):八仙數(shù)
九位自冪數(shù):九九重陽(yáng)數(shù)
十位自冪數(shù):十全十美數(shù)
參考資料:
水仙花數(shù)——百度百科
求水仙花數(shù)的java程序代碼
public class Daffodil {
/**
*
* @param
* @return void
* @param args
* desc
*/
public static void main(String[] args) {
for (int n = 100; n 999; n++) {
int a = n / 100;
int b = (n % 100) / 10;
int c = n % 10;
if(Math.pow(a, 3)+Math.pow(b,3)+Math.pow(c,3)==n){
System.out.println(n);
}
}
}
}
網(wǎng)站題目:用java代碼求水仙花數(shù) java程序求水仙花數(shù)
文章位置:http://fisionsoft.com.cn/article/ddgpcpj.html


咨詢
建站咨詢
