四叶玫瑰数的VB怎么编?
2015-08-09
展开全部
四位数各位上的数字的四次方之和等于本身为四叶玫瑰数。
实现源码如下:
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.
实现源码如下:
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.
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
四位数是从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
直接上循环了,太晚了,说完睡觉去
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
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询