四叶玫瑰数的VB怎么编?
发布网友
发布时间:2022-05-12 13:47
我来回答
共3个回答
热心网友
时间:2023-10-09 21:29
Private Sub Command1_Click()
Me.Cls
Dim s As Single, j As Long, t As String, n As Long, m As Long
For j = 1000 To 9999
t = j
m = 0
For k = 1 To 4
n = Val(Mid(t, k, 1))
m = m + n * n * n * n
Next
If m = j Then
Print j; "为玫瑰花数"
s = s + j
End If
Next
Print "总和为:"; s
End Sub
热心网友
时间:2023-10-09 21:29
四位数各位上的数字的四次方之和等于本身为四叶玫瑰数。
实现源码如下:
program roseNumber;
var
a,b,c,d:longint;
function four(n:longint):longint;
begin
four:=n*n*n*n;
end;
begin
for a:=1 to 9 do
for b:=0 to 9 do
for c:=0 to 9 do
for d:=0 to 9 do
begin
if(1000*a+100*b+10*c+d=four(a)+four(b)+four(c)+four(d))then
begin
writeln(a,b,c,d);
end
end
end.
热心网友
时间:2023-10-09 21:30
四位数是从1001-9999
直接上循环了,太晚了,说完睡觉去
Private Sub Command1_Click()
Dim i As Long
Dim s As String
Dim sum As Long
Dim n1, n2, n3, n4 As Long
For i = 1001 To 9999
s = CStr(i)
n1 = CLng(Mid(s, 1, 1)) ^ 4
n2 = CLng(Mid(s, 2, 1)) ^ 4
n3 = CLng(Mid(s, 3, 1)) ^ 4
n4 = CLng(Mid(s, 4, 1)) ^ 4
sum = n1 + n2 + n3 + n4
If sum = i Then Print i
Next i
End Sub
我自己运行了,1001-9999只有三个数:
1634
8208
9474