Ⅰ 水仙花數求法 跪求指點
//求1到100000的水仙花數
public class NarcissisticNumber {
public static void main (String [] args) {
int count = 0;
for(int i=1; i<=100000; i++) {
if(judge(i) == true) {
count++;
System.out.print(i + " "); // 1 153 370 371 407
}
}
System.out.println();//換行
System.out.println("一共有 " + count + " 個水仙花數.");
}
//判斷一個數是否為水仙花數
public static boolean judge (int number) {
int count = String.valueOf(number).length() - 1;
//求出number的長度,如4位,則對應是10的3次方, 10的count次方
int level = 0; //每一個位數
int sum = 0; //每一個位數的三次方的和
boolean b = false; //默認為false,即默認為非水仙花數
int temp = number; //從高位到低位逐位截取,即4位數變位3位數,3位數變為2位數...
while (count >= 0) {
level = temp / ((int)Math.pow(10,count));
sum = sum + (int)Math.pow(level,3);
temp = temp % ((int)Math.pow(10,count)); //由千位數變位百位,百位變為十位...
count--;
}
if(sum == number) { //是水仙花數
return (b = true);
}
return b; //不是水仙花數,b默認為false
}
}
Ⅱ 求三位數水仙花數的演算法怎麼設計
演算法是從100到999中依次取出1個三位數進行枚舉水仙花數判斷,第一步:求出這個三位數的百位、十位、個位的數字分別存在3個變數中, 例如:三位數321,百位3存在x中,十位2存在y中,個位1存在
Ⅲ 水仙花數的求取方法(非高精度)
以下為在各種編程語言中實現求取水仙花數的方法(非高精度)。 program narcissistic_number;var a,b,c:integer;begin
for a:=1 to 9 do
for b:=0 to 9 do
for c:=0 to 9 do
if a*a*a+b*b*b+c*c*c=100*a+10*b+c then
writeln(100*a+10*b+c);
end.
或
program narcissistic_number;var a,b,c,d:integer;begin
for a:=100 to 999 do
begin b:=a mod 10; c:=a mod 100 div 10; d:=a div 100;
if b*b*b+c*c*c+d*d*d=a then
writeln(a);
end;
end.
或
program narcissistic_number;var a, b, c, i, t : integer;
begin i := 100; repeat a:=trunc(i/100);
b:=trunc(i/10) - a*10; c:=i-trunc(i/10) * 10;
t := a*a*a + b*b*b + c*c*c;
if i = t then
writeln(i,'=',a,'^3+',b,'^3+',c,'^3');
i := i + 1 until i > 999
end. C Print all the Narcissistic numberC between 100 and 999
WRITE(*,30)
DO 10 K=100,999 IA=K/100 IB=MOD(K,100)/10 IC=MOD(K,10)
IF(K.NE.IA**3+IB**3+IC**3) GOTO 10
WRITE(*,20)K, IA,IB,IC10 CONTINUE20
FORMAT(5X,4I4)30
FORMAT(5X,18HN=I**3+J**3+K**3)
STOP
END <%
dim a,b,c,d,m,n,z
i=1
for i=100 to 999
a=mid(i,1,1)
b=mid(i,2,1)
c=mid(i,3,1)
d=a*a*a
m=b*b*b
n=c*c*c
z=d+m+n
if z=i then
response.write z & <br>
end if
next
%>
Visual FoxPro 用表單實現法(只計3位)
方法一:
clear
for a=1 to 9
for b=0 to 9
for c=0 to 9
x=a*100+b*10+c
if x=a^3+b^3+c^3
?x
endif
endfor
endfor
endfor
方法二:
(1)創建表單Form1並添加文本框Text1與命令按鈕Command1
(2)修改Command1的Caption屬性為「計算並顯示」
(3)為Form1添加方法sxh
(4)修改方法sxh代碼如下
para x
x1=int(x%10)
x2=int(x/10)%10
x3=int(x/100)%100
if x=x1^3+x2^3+x3^3
return .t.
else
return .f.
endif
(5)為Command1的Click事件編寫如下的事件代碼:
thisform.currentx=thisform.width/2
thisform.currenty=thisform.height/2
thisform.print(水仙花數是:)
for m=100 to 999
thisform.text1.value=m
sure=thisform.sxh(m)
if sure=.t.
thisform.print(str(m,4)+space(3))
inkey(0.5)
endif
for 延遲=1 to 20000
yiru=2008610029
endfor
endfor
this.enabled=.f. 1-999999之間
REM Print all the Narcissistic numberREM between 1 and 999999FOR i = 1 TO 999999 e$ = STR$(i) a$ = MID(e$, 1, 1) b$ = MID(e$, 2, 1) c$ = MID(e$, 3, 1) d$ = MID(e$, 4, 1) a = VAL(a$) AND b = VAL(b$) AND c = VAL(c$) AND d = VAL(d$) IF i = a ^ 4 + b ^ 4 + c ^ 4 + d ^ 4 THEN PRINT i;NEXT iENDPB 實現的方法(只計3位數)
int s,a,b,c
for s=100 to 999
a=integer(s/100)
b=integer((s - a*100)/10)
c=s - integer(s/10)*10
if s=a^3+b^3+c^3 then
messagebox(,s)
end if
next (100-999)
var a,b,c,d:integer;begin for a:=100 to 999 do begin b := a div 100; c := a div 10 mod 10; d := a mod 10; if b*b*b+c*c*c+d*d*d=a then memo1.Lines.Add(inttostr(a)) endend; (100-999)
方法一:
for m=100:999
m1=fix(m/100);
m2=rem(fix(m/10),10);
m3=rem(m,10);
if m==m1^3+m2^3+m3^3
disp(m)
end
end
方法二(可現不定位數解):
n=Input[請輸入大於2的自然數n:];
For[i=10^(n-1),i<10^n-1,i++,
If[Total[IntegerDigits^IntegerLength]==i,
Print]]
BASH 腳本實現計算100-999之內數#!/bin/bashfor (( a=1; a<10; a++ ))do for (( b=0; b<10; b++ )) do for (( c=0; c<10; c++ )) do number1=$((a*100+b*10+c)) number2=$((a**3+b**3+c**3)) if [ $number1 -eq $number2 ]; then echo $number1 fi done donedone易語言代碼 求指定范圍內水仙花數
.版本 2
.子程序 取出水仙花數, 整數型, , 返回范圍內水仙花數個數,如果范圍過大將會十分耗時
.參數 起始數字, 整數型, , 從此數開始判斷是否為水仙花數
.參數 結束數字, 整數型, , 此數必須大於起始數字
.參數 保存得到水仙花數的數組, 整數型, 可空 數組
.局部變數 數字數組, 文本型, , 0
.局部變數 水仙花數, 整數型, , 0
.局部變數 總和, 整數型
.局部變數 計次, 整數型
.局部變數 計次2, 整數型
.如果真 (起始數字 > 結束數字)
清除數組 (保存得到水仙花數的數組)
返回 (0)
.如果真結束
.變數循環首 (起始數字, 結束數字, 1, 計次)
.計次循環首 (取文本長度 (到文本 (計次)), 計次2)
加入成員 (數字數組, 取文本中間 (到文本 (計次), 計次2, 1))
處理事件 ()
.計次循環尾 ()
.計次循環首 (取數組成員數 (數字數組), 計次2)
總和 = 總和 + 求次方 (到數值 (數字數組 [計次2]), 取文本長度 (到文本 (計次)))
處理事件 ()
.計次循環尾 ()
.如果真 (總和 = 計次)
加入成員 (水仙花數, 計次)
.如果真結束
處理事件 ()
.變數循環尾 ()
保存得到水仙花數的數組 = 水仙花數
返回 (取數組成員數 (水仙花數))
BAT 計算100~999之間的水仙花數
@echo off&setlocal enabledelayedexpansion
for /l %%a in (1,1,9) do (
for /l %%b in (0,1,9) do (
for /l %%c in (0,1,9) do (
set /a ver1=%%a%%b%%c,ver2=%%a*%%a*%%a+%%b*%%b*%%b+%%c*%%c*%%c
if !ver1! == !ver2! echo !ver1!)))
pause>nul stack segment stack
dw 256 p (?)
stack ends
data segment
buf db 3 p (?)
data ends
code segment
assume cs:code,ds:data,ss:stack
fj proc near
push cx
push dx
push si
mov si,0
mov cx,10
fj1:
mov dx,0
div cx
mov buf[si],dl
inc si
or ax,ax
jnz fj1
mov ax,si
pop si
pop dx
pop cx
ret
fj endp
print proc near
push cx
push dx
mov dx,-1
push dx
mov cx,10
p1:
mov dx,0
div cx
push dx
or ax,ax
jnz p1
p2:
pop dx
cmp dx,-1
je p9
add dl,30h
mov ah,2
int 21h
jmp p2
p9:
mov dl,9
mov ah,2
int 21h
pop dx
pop cx
ret
print endp
start:
mov ax,data
mov ds,ax
mov bx,100
s0:
mov ax,bx
call fj
mov cx,ax
mov si,0
mov dx,0
s1:
mov al,buf[si]
mul buf[si]
mul buf[si]
add dx,ax
inc si
loop s1
cmp dx,bx
jne s2
mov ax,dx
call print
s2:
inc bx
cmp bx,1000
jb s0
mov ah,4ch
int 21h
code ends
end start 思想:枚舉n位數字中0——9出現的次數,根據枚舉的次數算出一個數,對比這個數中0——9出現的次數是否等於枚舉的0——9出現的次數相等。如果相等,則該數是水仙花數。 #include<stdio.h>#include<string.h>#include<vector>#include<string>#include<iostream>#include<algorithm>usingnamespacestd;#defineDIGIT21//每次就只用改變這里,就可以算出不同位數的水仙花數了。如果要想算出所用的,這里就寫最大,然後再在程序里加一層循環就是咯intCount[10];//Count用來保存枚舉是0——9出現的次數,用以和算出來的值各數字出現次數進行對比。intcnt1,num1[10][DIGIT+1][DIGIT+1];//cnt1是符合條件的個數。num1用來保存0——9分別出現0——DIGIT次對應的答案charans[10][DIGIT+1];//保存符合條件的答案//這兩個就是為了排序方便一點vector<string>v;strings[10];voiddeal(){intcnt[10];//保存算出來的數0——9出現的次數,用以和Count對比看是否滿足條件intno[DIGIT+1];//算出來的數memset(no,0,sizeof(no));memset(cnt,0,sizeof(cnt));for(intk=1;k<10;k++){if(num1[k][Count[k]][DIGIT]!=0){return;}for(inti=0;i<DIGIT;i++){no[i]+=num1[k][Count[k]][i];if(no[i]>9){no[i+1]+=(no[i]/10);no[i]%=10;}}}if(no[DIGIT]!=0){return;}if(no[DIGIT-1]!=0){intflag=0;for(intj=0;j<DIGIT;j++){cnt[no[j]]++;}for(intj=0;j<10;j++){if(cnt[j]!=Count[j]){flag=1;break;}}if(!flag){ans[cnt1][DIGIT]='