题型:填空题 题类: 难易度:普通
浙江省绍兴市2023-2024学年高二上学期信息技术期中考试选考试卷
编写程序,计算修改括号序列使其配对的最少次数。部分Python程序如下,请在划线处填入合适的代码。
s=input() #输入括号序列,序列中仅包含“(”、“)”两种字符,且长度为偶数
x=0
ans=0
for i in range(len(s)):
if s[i]=="(":
elif s[i]==")" and x>=1:
x-=1
elif s[i]==")" and :
ans+=1
x+=1
ans+=
print(ans)
s=input() stack=[0]* len(s);top=- 1;presign='+';num=0 for i in range(len(s)): if'0'<=s[i]<='9': num=num*10+int(s[i]) if i==len(s)- 1 or s[i] in'+-*/': if presign=='+': top+=1 stack[top]=num elifpresign=='-': top+=1 stack[top]=-num elifpresign=='*': top+=1 stack[top]=stack[top- 1]*num else: top+=1 stack[top]=stack[top- 1]//num presign=s[i] num=0 print(sum(stack)) #sum 函数对 stack 中所有元素求和 |
若输入'5*4-6+10/3' ,程序运行后, 输出结果是( )
a=[i for i in range(1,7)] b=[0]*6 head, tail=0,0 for i in range(1,7): cnt=1 while cnt<i: a[tail]=a[head] head=(head+1)%6 tail=(tail+1)%6 cnt+=1 b[a[head]- 1]=i head=(head+1)%6 |
执行该程序段后, b[5]的值为( )
试题篮