㈠ 有C#編寫查找10000內的水仙花數。
例:
for (int i = 100; i < 1000; i++) { int = 0; int shi = 0; int ge = 0; int yushu = 0; = i / 100; yushu = i % 100; shi = yushu / 10; ge = yushu % 10; if (i == * * + shi * shi * shi + ge * ge * ge) { Response.Write("水仙花數:" + i + "<br>"); } }
㈡ VB編寫程序,求100到1000之間所有水仙花數,編寫Function過程判斷是否是「水仙花數」。
一個form,一個按鈕
PrivateSubCommand1_Click()
Fori=100To1000'如果算1000這個數的話就這樣寫,不算的話就to999
sxhs(i)
Nexti
EndSub
Functionsxhs(numAsInteger)
If((num100)^3)+((numMod100)10)^3+(numMod10)^3=numThen
Printnum;
EndIf
EndFunction
效果圖
㈢ VB編程,找出所有的「水仙花數」。
這個程序在VB中與VC中是有些些不同的,因為VC中當定義每位為整型時,它不會有四捨五入規則的,而VB中則不同。比如:VC中(153/100)%10=1,而VB中它算出來的為2,所以注意這個就可以了。在VB中就先從個位判斷,是否超過或等於5,是的話就讓它減去0.5,不是的話直接操作。
VB中程序如下:一個按鈕,一個標簽就可以了。
Dim m As Integer
Dim m1 As Integer
Dim m2 As Integer
Dim m3 As Integer 'm,m1,m2,m3最好是分開定義不然會出現ByRef參數類型不符的錯誤
Dim str As String
Private Sub Command1_Click()
For m = 100 To 999
m3 = m Mod 10
If m3 >= 5 Then
m2 = (m / 10 - 0.5) Mod 10 '因為當個位數大於5時,m/10會進一位,即十位會比原來的數據多1,所以要把這個四捨五入的位取消五入規則
Else
m2 = (m / 10) Mod 10
End If
If m2 >= 5 Then
m1 = m / 100 - 0.5 '原因同上
Else
m1 = m / 100
End If
If ((cubic(m1) + cubic(m2) + cubic(m3)) = m) Then
str = str + CStr(m) + vbCrLf '把每計算出的數據存到str中,並換行
Label2.Caption = str
End If
Next m
End Sub
Function cubic(data As Integer) As Double
cubic = data * data * data
End Function
結果如下圖:
㈣ 用vb編程 水仙花數 求程序。萬分感謝
Private Sub Command1_Click()
For i = 100 To 999
a = Mid(i, 1, 1)
b = Mid(i, 2, 1)
c = Mid(i, 3, 1)
If a ^ 3 + b ^ 3 + c ^ 3 = i Then
Debug.Print i & "=" & a & "^3+" & b & "^3+" & c & "^3"
End If
Next i
End Sub
㈤ 用C#語言,100到1000「水仙花」數!!
for (int i = 100; i < 1000; i++) { int = 0; int shi = 0; int ge = 0; int yushu = 0; = i / 100; yushu = i % 100; shi = yushu / 10; ge = yushu % 10; if (i == * * + shi * shi * shi + ge * ge * ge) { Response.Write("水仙花數:" + i + "<br>"); } } 追問: Response.Write(" 水仙花數 :" + i + "<br>"); 為什麼不是Console.WriteLine("水仙花數:"+i+"<br>"); <br>是什麼意思?? 回答: Console.WriteLine()是輸出到屏幕的,一般用在控 制台 程序中,而且輸出的是一行。下一個輸出在下一行顯示。 Response.Write()是輸出到網頁的,一般用在WebSite或者WebApplaction中,輸出的不是一行。下一個輸出接續上一個輸出的末尾,加上<br>是為了換行。
記得採納啊
㈥ 水仙花數的求取方法(非高精度)
以下為在各種編程語言中實現求取水仙花數的方法(非高精度)。 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]='