法一:import java.awt.*;

創(chuàng)新互聯(lián)建站專注于仁壽網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供仁壽營銷型網(wǎng)站建設(shè),仁壽網(wǎng)站制作、仁壽網(wǎng)頁設(shè)計(jì)、仁壽網(wǎng)站官網(wǎng)定制、微信小程序定制開發(fā)服務(wù),打造仁壽網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供仁壽網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
import java.applet.*;
import java.math.*;
public class Shuixianhua extends Applet {
public void init() {
}
public void paint(Graphics g) {
//定義相關(guān)變量
int elem[]=new int[4];
int num,temp;
double total;
int row=20,column=30;
int count=0,k;
g.drawString("4位的水仙花數(shù)如下所示:", 20, 30 );
//利用循環(huán)尋找1000到10000之間的水仙花數(shù)
for(num=1000; num10000; num++)
{
k=0;
temp=num;
//提取num中的千位,百位,十位,個(gè)位,存儲(chǔ)在整型數(shù)組elem[4]中
do
{ elem[k]=temp%10;
temp=temp/10;
k++;
}while(!(temp==0));
total=Math.pow(elem[0],4)+Math.pow(elem[1],4)+Math.pow(elem[2],4)+Math.pow(elem[3],4);
//判斷是否未水仙花數(shù)
if(total==num)
{
count++;
//輸出格式控制
if(count%8==0)
{
row=row+25;
}
else
{
column=column+30;
}
g.drawString(num+"是水仙花數(shù)",row,column);
}
column=column+30;
g.drawString("共"+count+"個(gè)",row,column);
}
}
法二:public class Sxhs{
public static void main(String[] agrs){
int a1 , a2 , a3;
for(int i=1000 ; i10000 ; i++){
a1 = i / 1000;
a2=(i-a1*1000)/100;
a3=i-a1*1000-a2*100;
if(i==(a1*a1*a1)+(a2*a2*a2)+(a3*a3*a3)){
System.out.println("shi : " + i);
}
}
}
}
法三:public class shuixianhua{
public static void main(String[] agrs){
int a1 , a2 , a3,a4;
for(int i=1000 ; i10000 ; i++){
a1 = i / 1000;
a2=(i-a1*1000)/100;
a3=(i-a1*1000-a2*100)/10;
a4=i-a1*1000-a2*100-a3*10;
if(i==(a1*a1*a1*a1)+(a2*a2*a2*a2)+(a3*a3*a3*a3)+(a4*a4*a4*a4)){
System.out.println("shi : " + i);
System.out.println(a1);
System.out.println(a2);
System.out.println(a3);
System.out.println(a4);
}
}
}
}
法四:public class Suixian {
public static void main(String[] args) throws java.io.IOException {
byte[] buf = new byte[20];
int cmdLength = System.in.read(buf);
String str = new String(buf,0,cmdLength-2);
int n = Integer.parseInt(str);//這里當(dāng)然是輸入一個(gè)位數(shù)羅,也不一定就三位吧
int low = 1,high = 1;
for (int i=1;in;i++) low = low*10;
high = low * 10;
//System.out.println(low);
//System.out.println(high);
for (int i=low;ihigh;i++)
{
int sum = 0;
int p = i;
while (p!=0)
{
int r = p%10;
p = p/10;
int rn = 1;
for (int j=1;j=n;j++) rn = rn * r;
sum = sum + rn;
}
if (sum==i) System.out.println;
}
}
}
不過這只是法一的一個(gè)化簡,(法一用了一個(gè)數(shù)組)
第二個(gè)方法是這樣,拿三未數(shù)舉例
public class SuiXian2 {
public static void main(String[] args) {
for (int a=1;a=9;a++)
for (int b=0;b=9;b++)
for (int c=0;c=9;c++)
if(a*a*a+b*b*b+c*c*c==100*a+10*b+c)
System.out.println(100*a+10*b+c);
}
}
按一下代碼執(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ù):九九重陽數(shù)
十位自冪數(shù):十全十美數(shù)
參考資料:
水仙花數(shù)——百度百科
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);
}
}
}
}
求水仙花數(shù)的Java程序如下
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代碼 用java寫水仙花數(shù)代碼
文章源于:http://www.yijiale78.com/article16/ddocogg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、品牌網(wǎng)站建設(shè)、網(wǎng)站改版、響應(yīng)式網(wǎng)站、動(dòng)態(tài)網(wǎng)站、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)