把n转化为r进制并显示在标签Label1上。
s = ""
n = Val(Text1.Text)
r = Val(Text2.Text)
Do While n > 0
Loop
Label1.Caption = s
方框中的代码由以下三部分组成:
① n = n \ r ② t = n Mod r ③ s = Chr(Asc("0") + t) + s
下列选项中代码顺序正确的是( )
s1 = "PROGRAM": s2 = ""
For i = 1 To 4
t = Int(Rnd * 4) * 2 + 1
Do While f(t)
t = Int(Rnd * 4) * 2 + 1
Loop
f(t) = True
s2 = s2 + Mid(s1, t, 1)
Next i
Label1.Caption = s2
数组f 各元素的初值为False,执行该程序段后,标签Label1上显示的内容可能是( )
d(0) = 100
For i = 1 To 5
k = i
For j = i + 1 To n
If d(j) < d(i - 1) Then
If ① Then k = j
End If
Next j
If i <> k Then t = d(i): d(i) = d(k): d(k) = t
Next i
则代码中①处的语句为( )
For p = 1 To 6
i = 1: j = 6: q = 0
Do While i <= j
m = (i + j) \ 2
q = q + 1
If p = m Then
f(p) = q: Exit Do ' Exit Do 表示退出循环
End If
If p > m Then i = m + 1 Else j = m - 1
Loop
Next p
数组元素f(1)到f(6)初始为0,执行该程序段后,其值依次是( )
Const m = 6, n = 8, wt = 200
Dim d(1 To m + n) As String, a(1 To m + n) As Integer
Private Sub Form_Load()
'读取m + n 个人的体重及去向数据存入d 数组,代码略
End Sub
Private Sub Command1_Click()
Dim i As Integer, p As Integer, q As Integer, s As String
Dim num As Integer, num1 As Integer, num2 As Integer
'以下代码实现从d 数组中提取每个人的体重数据,根据去向分段存入a 数组
p = 1 : q =
For i = 1 To m + n
s = Mid(d(i), 1, Len(d(i)) - 1)
If Mid(d(i), , 1) = "W" Then
a(p) = Val(s): p = p + 1
Else
a(q) = Val(s): q = q + 1
End If
Next i
num1 = GetNum(1, m)
num2 = GetNum(m + 1, m + n)
If num1 > num2 Then num = num1 Else num = num2
Label1.Caption = "过河需要最少往返次数:" + Str(num)
End Sub
Function GetNum(head As Integer, tail As Integer) As Integer
Dim i As Integer, j As Integer, k As Integer, cnt As Integer, t As Integer
'以下代码实现对数组a 降序排序
i = head
Do While i < tail
k = i: i = tail
For j =
If a(j - 1) < a(j) Then
t = a(j): a(j) = a(j - 1): a(j - 1) = t
i = j
End If
Next j
Loop
cnt = 0: i = head: j = tail
Do While i <= j
If Then j = j - 1
cnt = cnt + 1: i = i + 1
Loop
GetNum = cnt
End Function
求T值的方法如下:取某个灰度值k,将图像所有N个像素分成两类,灰度值小于k的作为背景像素,灰度值大于等于k 的作为前景像素,统计两类像素的个数分别为N1、N2,计算灰度值的均值分别为μ1、μ2,利用公式 计算出两类像素的差异值。当该值最大时,k即为要找的最佳灰度值T。若某区间段内有连续多个T满足要求,则T取中间值。
例如:图a所示图像像素的灰度值仅为0~5,统计0~5灰度值像素的个数依次为8、7、2、6、9、4,如图b所示。当k=2时,N1=15,N2=21,μ1=(0*8+1*7)/(8+7),μ2=(2*2+3*6+4*9+5*4)/(2+6+9+4),计算出差异值为2.5635。依此类推,计算差异值如图c所示。可知,当k=3时差异值最大,3是最佳灰度值T。
编写VB程序,实现如上功能。读取图像所有像素的灰度值后,单击按钮Command1,在标签Label1上显示T值。
Const N = 32 * 24
Dim a(1 To N) As Integer 'a 数组存储图像所有像素的灰度值
Private Sub Form_Load()
'读取图像中的所有像素的灰度值存入a数组,代码略
End Sub
Private Sub Command1_Click()
Dim i As Integer, j As Integer, k As Integer
Dim sum As Long, sum1 As Long, N1 As Long, N2 As Long
Dim μ1 As Double, μ2 As Double, diff As Double, max_var As Double
Dim t1 As Integer, t2 As Integer, T As Integer
Dim h(255) As Long
Dim c(255) As Long
max_var = 0: t1 = 0: t2 = 0: N1 = 0: N2 = 0: sum = 0: sum1 = 0
For i = 0 To 255
h(i) = 0: c(i) = 0
Next i
For i = 1 To N
h(a(i)) = h(a(i)) + 1
Next i
c(0) = h(0)
For i = 1 To 255
'统计灰度值小于等于i 的像素个数
sum = sum + i * h(i)
Next i
For k = 1 To 255
N1 = c(k - 1)
N2 = N - N1
If N1 <> 0 And N2 <> 0 Then
μ1 = sum1 / N1
μ2 = (sum - sum1) / N2
diff = N1 / N * N2 / N * (μ1 - μ2) ^ 2
diff = Round(diff, 4) 'Round 函数的作用是对diff 保留四位小数
If diff >= max_var Then
t1 = k
If diff > max_var Then
max_var = diff
End If
End If
Next k
T = (t1 + t2) \ 2
Label1.Caption = "将该图像分割为前景与背景两部分的最佳灰度值为:" & T
End Sub