VB开灯关灯的编程问题
有N个灯放在一排(N不大于100),从1到N依次顺序编号。有N个人也从1到N依次编号。1号将灯全部关闭,2号将凡是2的倍数的灯打开,3号将凡是3的倍数的灯作相反处理(该灯...
有N个灯放在一排(N不大于100),从1到N依次顺序编号。有N个人也从1到N依次编号。1号将灯全部关闭,2号将凡是2的倍数的灯打开,3号将凡是3的倍数的灯作相反处理(该灯如为打开的,则将它关闭;如关闭的,则将它打开)。以后的人都和3号一样,将凡是自己编号的倍数的灯作相反处理。计算第N个人操作后,各盏灯的状况(即开关情况)并输出。
一定要复制上去直接可以运行成功的,速度在线求答案 展开
一定要复制上去直接可以运行成功的,速度在线求答案 展开
展开全部
Private Sub Command1_Click()
Dim d() As Boolean, N
N = Val(InputBox("N="))
ReDim d(N)
For i = 1 To N
If i = 1 Then
For j = 1 To N
d(j) = False
Next
Else
For j = 1 To N
If j Mod i = 0 Then d(j) = Not d(j)
Next
End If
Next
For i = 1 To N
Print "灯"; i; ":";
If d(i) Then Print "开" Else Print "关"
Next
End Sub
Dim d() As Boolean, N
N = Val(InputBox("N="))
ReDim d(N)
For i = 1 To N
If i = 1 Then
For j = 1 To N
d(j) = False
Next
Else
For j = 1 To N
If j Mod i = 0 Then d(j) = Not d(j)
Next
End If
Next
For i = 1 To N
Print "灯"; i; ":";
If d(i) Then Print "开" Else Print "关"
Next
End Sub
展开全部
有什么问题,那里报错
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Dim a(100) string '灯的个数
Dim b = 5 '人的个数
For Each i In b
If i = 0 then
For Each j In a.length
a(j) = '关'
Next
elseif i = 1 then
For Each j In a.length
If (j+1)%2 = 0 then
a(j) = '开'
end if
Next
else
For Each j In a.length
If (j+1)%(i+1) = 0 then
If a(j) = '关' then
a(j) = '开'
else
a(j) = '关'
end if
end if
Next
end If
Next
For Each m In a
wscript.echo m
Next
Dim b = 5 '人的个数
For Each i In b
If i = 0 then
For Each j In a.length
a(j) = '关'
Next
elseif i = 1 then
For Each j In a.length
If (j+1)%2 = 0 then
a(j) = '开'
end if
Next
else
For Each j In a.length
If (j+1)%(i+1) = 0 then
If a(j) = '关' then
a(j) = '开'
else
a(j) = '关'
end if
end if
Next
end If
Next
For Each m In a
wscript.echo m
Next
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询