移红包规则:
①编号为1的堆上的红包,只能移到编号为2的堆上;
②编号为n的堆上的红包,只能移到编号为n-1的堆上;
③其它堆上的红包,可以移到相邻左边或右边的堆上。
现要求找出最少的移动次数使每堆的红包数-样多。如图所示,在文本框Text1中输入红包堆数n,在文本框Text2中依次输入每堆红包的红包个数(以“,”为分隔符和结束符),单击“移红包次数统计”按钮Command1后,在标签Labe13中显示最少的移动次数。
Const Sum = 40 ‘红包总个数
Private Sub Command1_Click()
Dim n As Integer ‘红包的堆数
Dim s As String, k As Integer
Dim avg As Integer '最终每堆的红包个数
Dim ml As Integer, m2 As Integer
Dim tmp As Integer, count As Integer, ans As Integer
n=
s = Text2. Text
avg=Sum/n
tmp=0:count=0:ans=0
For k=1 To Len(s)
c = Mid(s, k, 1)
If c <>”,”Then
tmp =
Else
count = count + 1
If count = 1 Then
m1 =tmp
Else
m2 = tmp
If m1 <> avg Then
m2=m2-(avg-m1)
ans=ans+1
End If
End If
tmp=0
End If
Next k
Labe13. Caption = Str (ans)
End Sub
Const n =6
Max = 0
For i = 1 To n
d(i)= 1
For j = 1 To i - 1
If a(j)< a(i) And d(j)+ 1 > d(i) Then d(i)= d(i)+ 1
Next j
If d(i)> Max Then Max = d(i)
Next i
数组元素a(1)到a(6)的值依次为“2,40,15,1,25,37”,执行该程序段,变量max的值为( )
s=0
for i in range (1,5):
s=s+i
print("i=",i,"s=",s)
Const n = 10
Dim d(n) As Integer, c As Integer
Private Sub Form_Load()
Dim i As Integer, j As Integer, f As Boolean, s As String
Randomize
c = n
For i = 1 To n
f = False
d(i) = ' 随机生成个位数为“9”的两位数
For j = 1 To i - 1
If Then f = True
Next j
If f Then c = c - 1
Next i
'原始数据在文本框Text1中显示。代码略
End Sub
Private Sub Command1_Click()
Dim i As Integer, j As Integer, k As Integer, m As Integer
Dim cnt As Integer,t As String, s As String
d(0) = 100
cnt = Val(Text2.Text)
If cnt > c Then
Text3.Text = "输入筛选的个数太大,请重新输入!"
Exit Sub 'Exit Sub表示退出过程
End If
For i = 1 To cnt
k = i
For j = i + 1 To n
If d(j) < d(i - 1) And( )Then k = j
Next j
If k <> i Then
t = d(i): d(i) = d(k): d(k) = t
End If
Next i
For i = 1 To cnt
s = s + Str(d(i)) + ","
Next i
Text3.Text =
End Sub
Do While i<10
k=1
i=i+1
Do While k<10
s=s+1
k=k+i
Loop
Loop
a=1:b=2
For i=1 To 6
c=a+b
a=b:b=c
k=2
Do While k<c
If c Mod k=0 Then Exit Do
k=k+1
Loop
If k=c Then s=s+1
Next i
例如,输入字符串“addded”点击“运行”按钮后输出字符串“bd”;输入字符串“adddedb”,点击“运行”按钮后输出空字符串:输入字符串“abbba”点击“运行”按钮后输出字符串“bea”。
Private Sub Command1_Click( )
Din s1 As String, s2 As String
Dim i As Integer, j As Integer
S1 = Text1.Text
Do While Len(s1) > 1
s2=“”:i=1
Do While i<= Len(s1)
For j= i +1 To Len(s1)
If Mid(s1, i, 1) < Mid(s1, j, 1) Then Exit For
Next j
If j=i+1 Then s2= s2 + Mid(s1, i, 1)
Loop
If s2= s1 Then Exit Do
s1 =
Loop
Label1.Caption = s1
End Sub
1)将明文中的字母E替换为A,F替换为B,G替换为C,…A替换为W,B替换为X,C替换为Y,…,小写字符的替换方法同理。
如:明文:I Love China 加密为:E Hkra Ydejw
2)将加密后的英文字母,按顺序依次放入每个字符串段落中,第1个加密字母E放在第1个段落,第2个字母H放在第2个段落,依次类推(如下图),段落间用逗号分隔并以逗号结尾。当遇到空格,在段落末尾“,”前,插入“+”号作为标记。
第1段 | 第2段 | 第3段 | 第4段 | 第5段 | 第6段 | 第7段 | 第8段 | 第9段 | 第10段 |
E+, | H, | k, | r, | a+, | Y, | d, | e, | j, | w+, |
3)每个段落中,随机插入字母形成新的字母序列段,当段号小于8时加密字母在字母序列
中存放位置刚好是该段落号,位置以8为周期,第9段时,加密字母存放位置回到字母序列的第1位,第10段加密字母存放位置回到第2位,依次类推。(位置号<=8)
第1段 | 第2段 | 第3段 | 第4段 | 第5段 |
Etc+, | mHfryh, | oTkrestf, | rfir, | Ccfta+, |
第6段 | 第7段 | 第8段 | 第9段 | 第10段 |
iynhyYf, | tqswjyd, | nrtesiueh, | jnytq, | Uwccdb+, |
4)在每一段中随机插入除英文字母之外的ASCII字符码,形成一串密文。如图所示:
则根据加密算法,解密后的明文为
Private Sub Command1_Click()
Dim i As Integer, k As Integer
Dim s As String, c As String, ch As String
Dim letter As String, ret As String 'letter 用于存放字母序列s = Text1.Text
k = 1
For i = 1 To Len(s)
c = Mid(s, i, 1)
If c >= "A" And c <= "Z" Or c >= "a" And c <= "z" Then letter = letter + c
ElseIf
c = ","
Then
ch = Chr(Asc(ch) + 4)
If Not (ch >= "A" And ch <= "Z" Or ch >= "a" And ch <= "z") Then ch = Chr(Asc(ch) - 26)
End If
IfThen ret = ret + ch + " "
Else
ret = ret + ch
End If
k = k + 1
End If
Next i
Text 2.Text = ret
End Sub
Private Sub Command1_Click()
Dim s As String, ch As String
Dim n As Integer, t As Integer, c As Integer
s = "Go-2020-12"
n = Len(s):t = 0
For i = 1 To n
ch = Mid(s,i,1)
If ch >= "0" And ch <= "9" Then
t = t + Val(ch)
Else
c = t: t = 0
End If
Next i
Label1.Caption = Str(c)
End Sub
执行完以上程序段后,标签Label1上显示的内容为( )
amin = a(1): amax = a(1)
i = 2: j = 9
Do While i <= j
If a(i) > a(j) Then
Else
End If
i = i + 1: j = j - 1
Loop
Text1.Text = Str(amin) + "," + Str(amax)
上述程序段中方框处可选语句为:
①If a(j) > amax Then amax = a(j)
②If a(j) < amin Then amin = a(j)
③If a(i) < amin Then amin = a(i)
④If a(i) > amax Then amax = a(i)
则方框处语句依次可为( )
s = Text1.Text
ans = "": k =4
i = k - 1: j = k + 1: n = Len(s)
Do While k > 0
If Mid(s ,i,1) = Mid(s, j, 1) Then
ans = ans + Mid(s, i,1): k = k - 1
End If
i = (n + i - 2) Mod n + 1
j = j Mod n+1
Loop
Label1.Caption=ans
在文本框Text1中输入“banana”,执行程序后,标签Label1中显示的内容是( )
L = [1,-2,3,7,-8,-9,10]
j=len(L)-1
i = 0
k = 0
while i<j:
while L[i]>0:
i=i+1
while L[j]<0:
j=j-1
if i<j:
k = L[i]
L[i]=L[j]
L[j] = k
print(L)
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
i = 1: L = 0: R = n + 1
Do While i < R
If a(i) < k1 Then
L = L + 1
If L <> i Then tt = a(i): a(i) = a(L): a(L) = tt
i = i + 1
ElseIf a(i) > k2 Then
R = R - 1
tt = a(i): a(i) = a(R): a(R) = tt
Else
i = i + 1
End If
Loop
则执行上述代码后,下列说法正确的是( )
s = 'wenzhou'; s1 = ''; n= len(s)
for i in range(n//2,0,-1) :
if s[i-1] < s[n-i] :
s1 = s1 + s[i-1]
else:
s1=s1+s[n-i]
print (s1)
运行程序后,输出的内容是( )
Private Sub Command1_Click()
Dim a(1 To 3) As Integer, k As Integer, t As Integer, s As Integer
a(1) = 12: a(2) = 41: a(3) = 2
s = 0
For k = 2 To 3
If a(k) > a(1) Then
t = a(1): a(1) = a(k): a(k) = t
s = s + 1
End If
Next k
Text1.Text = Str(a(1))
Text2.Text = Str(s)
End Sub
根据上述要求编写VB程序,功能如下:读取某天上午办理业务的num个客户信息(已按到达时间顺序排列),显示在列表框List1中。程序运行时,在文本框Text1中输入可同时办理业务的窗口数量m,单击“输出”按钮Command1,在列表框List2中输出客户平均等待时间、等待时间最长的客户编号及其等待时间。程序运行界面如第16题图所示。
请回答下列问题:
Const n = 300
Dim no(1 To n) As String, st(1 To n) As String, ti(1 To n) As Integer
Dim num As Integer 'num为待办理业务总人数
Private Sub Form_ load ()
'从数据库中读取所有客户编号、到达时间和办理业务所需要时间等信息,分别保存在
'数组no、st和ti中,统计总人数num,并在列表框List1中显示客户信息,代码略。
End Sub
Function t2d(x As String) As Integer
'将时间转化成从0点开始的时长(单位:分钟)来表示,例如8:18用498来表示;
'代码略
End Function
Private Sub Command1_ Click ()
Dim max_ no As String,max_ wait As Integer
Dim wait_ sum As Integer, wait_ time As Integer, wait_ num As Integer
Dim m As Integer, k As Integer, t As Integer
Dim w(1 To 10) As Integer '数组w存储窗口办理业务结束的时间(从0点开始的时长)
Dim i As Integer, p As Integer, q As Integer
m = Val (Text1. Text)
wait_ sum = 0
wait_ num = 0
max_ wait = 0
max_ no =""
k=0
For i=1 To m
w(i) = 0
Next i
For i=1 To num
If Then '当前办理业务不需要等待
t = t2d(st(i)) + ti(i)
Else
wait_ num = wait_ num + 1
wait_ time = w(1) - t2d(st (i))
wait_ sum = wait_ sum + wait_ time
If wait_ time > max_ wait Then
max_ wait = wait_ time
max_ no = no(i)
End If
End If
p =1
Do While w(p)<t And p<=k
p=p+1
Loop
If k<m Then '当前办理业务不需要等待
q=k
Do While q > = p
w(q+1)=w(q)
q=q-1
Loop
w(p) = t
k=k+1
Else
q=1
Do While q< p -1
q=q+1
Loop
w(q) = t
End If
Next i
List2. AddItem "平均等待时间:"+Str (Int (wait_ _sum/wait_ _num*10+0. 5)/10)+*分钟"
List2. AddItem "等待时间最长的是:"+max_ no+"号, 等待了"+Str (max_ wait)+^ 分钟"
End Sub
运行输出结果是:
s1=input("请输入字符串:")
a=[0]*128
for item in s1:
ch=ord(item)
a[ch]=a[ch]+1
s2=""
for i in range(len(a)):
for j in range(a[i]):
s2=s2+chr(i)
print(s2)
列表a各元素的初始值都为0,s1中输入的内容为“abs54int”。执行该程序后,程序输出的结果中第5个字符为( )