组卷网 > 高中信息技术综合库 > 数据与计算 > 算法与程序实现 > 算法 > 典型算法
题型:操作题 难度:0.4 引用次数:28 题号:9654925
【加试题】在数据压缩中,一个常用的途径是行程长度压缩。对于一个待压缩的字符串而言,我们可以依次记录每个字符及重复的次数。这种压缩,对于相邻数据重复较多的情况比较有效。 例如,如果待压缩串为”AAABBBBCBB”,则压缩的结果是(A,3)(B,4)(C,1)(B,2)。
现要求根据输入的字符串(长度在1000 以内),全部由数字和字母组成,得到大小写不敏感压缩后的结果(即所有小写字母均视为相应的大写字母)。如:原字符串为aAABBbBCCCaaaaa,压缩后的结果(A,3)(B,4)(C,3)(A,5)。
实现这一功能的程序代码如下:
Private Sub Command1_Click()
 Dim s As String, s1 As String
 Dim tmp As String, ans As String
 Dim i As Integer, j As Integer
s = Text1.Text
s1 = ""
For i = 1 To Len(s)
  s1 = s1 & ToUpcase(   )
Next i
i = 1
Do While i <= Len(s1)
tmp = Mid(s1, i, 1)
ans = ans & "(" & tmp & ","
j = i + 1
Do While j <= Len(s1) And Mid(s1, j, 1) = tmp
j = j + 1
Loop
ans = ans &           & ")"
   
 Loop
 Text2.Text = ans
End Sub
Function ToUpcase(c As String) As String
 If c >= "a" And c <= "z" Then
  ToUpcase = Chr(Asc(c) - 32)
 Else
  ToUpcase = c
 End If
End Function
在画线处填入适当的语句或表达式,将程序补充完整。
______________________________
【知识点】 典型算法

相似题推荐

操作题 | 较难 (0.4)
【推荐1】答题卡一般采用2B铅笔填涂,填涂好的答题卡经过扫描后得到如图所示的数字化图像,从一个像素点的灰度值>132判断是否被填涂开始,进而判断一个信息点是否被填涂。利用如下的Python程序可以识别并统计填涂好的答题卡中的答案:
(1)答题卡通常使用红色,如使用黑色印刷,对答案识别__________(有/无)影响。
(2)请在划线处填入合适代码。①____________________________________

from PIL import Image
x_start =11                                 #起始点坐标
y_start =92
fill_width =24                                 #信息点宽度
fill_height =10                                 #信息点高度
space_width =15                           #间隔宽度
space_height=12                           #间隔高度
ans_cnt =5                                 #题目个数
def bw_judge(R,G,B):             #bw_judge用于判断一个像素的填涂情况

Gray sca1e=0.299*R+0.587*G+0.114*B

return Gray_scale 132

def fill_judge(x,y):                    #fil1_judge用于判断信息点的填涂情况

count =0

for i in range(x,x+fill_width):

for j in range( ):

R,G,B=pixels[i,j]

if bw_judge(R,G,B)==True:

count =count+1

if count >fill_width * fill_height * 0.64:

return True

total_width = fill _width + space_width
total_height = fill_height+space_height
image=Image.open("card.bmp")
pixels=image.load()
ans = ""
item=['A','B','C','D']
list=[]
for col in range(ans_cnt):

x=

for row in range(4):

y=y_start + total_height * row

if fill_judge(x,y)=True:

list.append(ans)

ans=""

print(list)
2022-02-16更新 | 129次组卷
操作题 | 较难 (0.4)
【推荐2】为四则运算式“6+(8-2)*2÷3”转逆波兰表达“682-2*3÷+”设计算法, 编程实现。
分析:在数学运算表达式中,运算符总是置于与之相关的两个运算对象之间,在计算结果时,要考虑括号、运算符号的优先性。为了程序实现的方便,波兰逻辑学家J.Lukasiewicz提出了另 一种表示法,将运算符置于其运算对象之后,没有括号,不用考虑运算符号的优先性。这种表达 式称为后缀表达式,又叫逆波兰表达式。
如表达式“682-2*3÷+”是四则运算式“6+ (8-2) *2÷3”的逆波兰表达式。为了处理方便, 规定表达式中的数均为小于10的正整数,运算符为+ - * ÷。
(1) 抽象建模
设计两个栈bds、fh,栈bds用来存放表达式,栈fh用来暂时存放运算符。从左往右扫描四则运算式,遇到数字时,入栈bds;遇到运算符号时,根据运算符号的优先级设计进栈与出栈。
四则运算式“6+ (8-2) *2÷3”转换规则的模拟过程如表 1 所示:

表1

结合表1的操作过程,用栈bds和栈fh记录每个操作后的栈内情况(见图),那么在操作2中栈fh里有内容为(请从栈底到栈顶顺序书写)______

(2)设计算法
基于问题的抽象与建模,解决该问题的主要算法描述如下:
从左往右遍历四则运算式s(设中间变量为 ch):
1) 当ch是数字,直接入栈bds;
2) 当ch是运算符:
a.若ch为左括号时,直接入栈fh;
b.若ch为右括号时, 则将栈fh元素弹出,压入栈bds,直到遇到左括号(左括号只弹出,不压入栈bds);
c.若ch为其它运算符时, 如果运算符ch优先级大于栈fh中栈顶元素的优先级(或栈fh为空),直接入栈fh;否则,将栈fh元素依次弹出,并压入栈bds,直到运算符ch优先级大于栈fh中栈顶元素的优先级(或栈fh为空);
3)将栈bds中元素依次出栈,即为该四则运算s的后缀表达式。
(3) 编写程序
实现上述功能的Python代码如下,请在划线处填入合适代码。
yxj = {"+":1,"-":1,"*":2,"÷":2} #运算规则的优先级
s = input("请输入四则运算式: ")
fh = [""]*100   #存储运算符
topfh = -1
bds = [""]*100 #存储表达式
top=-1
for ch in s:

if ch.isdigit():     #字符串只包含数字则返回 True 否则返回 False

top+=1

bds[top]=ch

elif ch == "(":

topfh +=1

fh[topfh]=ch

elif ch == ")":

while True:

tmp = fh[topfh]

topfh-=1

if tmp=="(":

_______

top+=1

bds[top]=tmp

elif ch in yxj:

if topfh==-1 or fh[topfh]=="(":

topfh += 1

fh[topfh]=ch

elif_________:

topfh+=1

fh[topfh]=ch

else:

while fh[topfh]!="(" and topfh!=-1:

if yxj[fh[topfh]]>=yxj[ch]:

top+=1

bds[top]=fh[topfh]

topfh-=1

else:

break

topfh+=1

_______

while topfh!=-1:

top+=1

bds[top]=fh[topfh]

topfh-=1

print("后缀表达式:","".join(bds[:top+1]))
2022-07-26更新 | 65次组卷
操作题 | 较难 (0.4)
【推荐3】小龙同学设计了一个求四则混合运算结果的程序,用于计算不带括号的+、-、*、\的运算, \为整除运算,且此处要求*和\的优先级相同。在表达式中输入待求解的表达式,以=结尾。按下 Command1“计算”按钮后,在标签控件 Label1 中输出运算结果。程序界面如下图所示:

(1)在表达式中输入15-22\5*2+7=,则输出的结果是
(2)实现上述功能的 VB 程序如下,请在划线处填入合适代码。

Function calculate(x As Integer, y As Integer, op As String) As Integer If op = "+" Then calculate = x + y

If op = "-" Then calculate = x - y If op = "*" Then calculate = x * y If op = "\" Then calculate = x \ y

End Function

Function state(op As String) As Integer If op = "#" Then state = -1

If op = "=" Then state = 1

If op = "+" Then state = 1

If op = "-" Then state = 1

      '①

If op = "*" Then state = 3

End Function

Private Sub Command1_Click()

Dim s As String, length As Integer, t As String Dim k As Integer, q As Integer

Dim opt(0 To 3) As String Dim i As Integer

Dim s1 As String

Dim a(1 To 3) As Integer s = Text1.Text

length = Len(s)

t = "": k = 0: q = 0: opt(q) = "#"

For i = 1 To length s1 = Mid(s, i, 1)

If s1 >= "0" And s1 <= "9" Then

   t = t + s1

Else

k = k + 1 a(k) = Val(t) t = ""

Do While     '②

a(k - 1) = calculate(a(k - 1), a(k), opt(q))

k = k - 1

q= q - 1

Loop

q = q + 1

        '③

If s1 = "=" Then Label1.Caption = Str(a(1))

End If

Next i

End Sub
2020-07-22更新 | 54次组卷
共计 平均难度:一般