十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
法一:import java.awt.*;
創(chuàng)新互聯(lián)主要從事網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務。立足成都服務烏當,十余年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:18980820575
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中的千位,百位,十位,個位,存儲在整型數(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+"個",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);//這里當然是輸入一個位數(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;
}
}
}
不過這只是法一的一個化簡,(法一用了一個數(shù)組)
第二個方法是這樣,拿三未數(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);
}
}
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);
}
}
}
}
按一下代碼執(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);
}
}
}
}
擴展資料:
水仙花數(shù)只是自冪數(shù)的一種,嚴格來說3位數(shù)的3次冪數(shù)才稱為水仙花數(shù)。
一位自冪數(shù):獨身數(shù)
兩位自冪數(shù):沒有
三位自冪數(shù):水仙花數(shù)
四位自冪數(shù):四葉玫瑰數(shù)
五位自冪數(shù):五角星數(shù)
六位自冪數(shù):六合數(shù)
七位自冪數(shù):北斗七星數(shù)
八位自冪數(shù):八仙數(shù)
九位自冪數(shù):九九重陽數(shù)
十位自冪數(shù):十全十美數(shù)
參考資料:
水仙花數(shù)——百度百科
你的程序主要是每個數(shù)前,sum變量沒有置0
改正后的水仙花數(shù)的Java程序如下(改動的地方見注釋)
public class a99{
public static void main(String[] arge){
int sum=0,t=0;
for(int i=100;i1000;i++){
t=i;
sum=0;//這里加一句,每個數(shù)字都要初始化sum=0
while(t!=0){
sum += ((t%10)*(t%10)*(t%10));
t /= 10;
}
if(sum==i)
System.out.println(i+"是水仙花數(shù)");//這里printf改成println
}
}//這里加大括號
}//這里加大括號