十年網站開發(fā)經驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網站問題一站解決
法一:import java.awt.*;
專注于為中小企業(yè)提供網站建設、成都網站設計服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)和平免費做網站提供優(yōu)質的服務。我們立足成都,凝聚了一批互聯網行業(yè)人才,有力地推動了上1000家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網站建設實現規(guī)模擴充和轉變。
import java.applet.*;
import java.math.*;
public class Shuixianhua extends Applet {
public void init() {
}
public void paint(Graphics g) {
//定義相關變量
int elem[]=new int[4];
int num,temp;
double total;
int row=20,column=30;
int count=0,k;
g.drawString("4位的水仙花數如下所示:", 20, 30 );
//利用循環(huán)尋找1000到10000之間的水仙花數
for(num=1000; num10000; num++)
{
k=0;
temp=num;
//提取num中的千位,百位,十位,個位,存儲在整型數組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);
//判斷是否未水仙花數
if(total==num)
{
count++;
//輸出格式控制
if(count%8==0)
{
row=row+25;
}
else
{
column=column+30;
}
g.drawString(num+"是水仙花數",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);//這里當然是輸入一個位數羅,也不一定就三位吧
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;
}
}
}
不過這只是法一的一個化簡,(法一用了一個數組)
第二個方法是這樣,拿三未數舉例
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);
}
}
}
}
水仙花數是指一個?n?位數?(?n≥3?),它的每個位上的數字的?n?次冪之和等于它本身。(例如:1^3?+?5^3+?3^3?=?153)
按一下代碼執(zhí)行:
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次冪數才稱為水仙花數。
一位自冪數:獨身數
兩位自冪數:沒有
三位自冪數:水仙花數
四位自冪數:四葉玫瑰數
五位自冪數:五角星數
六位自冪數:六合數
七位自冪數:北斗七星數
八位自冪數:八仙數
九位自冪數:九九重陽數
十位自冪數:十全十美數
參考資料:
水仙花數——百度百科