早教吧作业答案频道 -->其他-->
pascal 括号配对问题假设表达式中允许包含两种括号:圆括号和方括号,其嵌套的顺序随意,如(〔 〕())或〔(〔 〕〔 〕)〕等为正确的匹配,〔(〕)或(〔 〕( )或 (()))均为错
题目详情
pascal 括号配对问题
假设表达式中允许包含两种括号:圆括号和方括号,其嵌套的顺序随意,如(〔 〕())或〔(〔 〕〔 〕)〕等为正确的匹配,〔(〕)或(〔 〕( )或 (()))均为错误的匹配.
现在的问题是,要求检验一个给定表达式中的括弧是否正确匹配?
输入一个只包含圆括号和方括号的字符串,判断字符串中的括号是否匹配,匹配就输出 “OK” ,不匹配就输出“Wrong”.
输入一个字符串:
〔(〔〕〔〕)〕
输出:
OK
var
a:string;
i,j:integer;
now:array[1..40]of char;
l:integer;
ans:integer;
top:integer;
begin
read(a);
l:=length(a);
ans:=0;
top:=0;
if (a[1]=')') or (a[1]=']') then
begin
ans:=1;
write('Wrong');
end;
while (a[i]' ') and (ans1) do
begin
if (a[i]='(') and (a[i]='[') then
begin
top:=top+1;
now[top]:=a[i];
end;
if (a[i]=')') then
if now[top]='(' then
top:=top-1
else
begin
write('wrong');
ans:=1;
end;
if (a[i]=']') then
if now[top]='[' then
top:=top-1
else
begin
write('Wrong');
ans:=1;
end;
i:=i+1;
end;
if ans=0 then write('OK');
end.
哪里错了?
假设表达式中允许包含两种括号:圆括号和方括号,其嵌套的顺序随意,如(〔 〕())或〔(〔 〕〔 〕)〕等为正确的匹配,〔(〕)或(〔 〕( )或 (()))均为错误的匹配.
现在的问题是,要求检验一个给定表达式中的括弧是否正确匹配?
输入一个只包含圆括号和方括号的字符串,判断字符串中的括号是否匹配,匹配就输出 “OK” ,不匹配就输出“Wrong”.
输入一个字符串:
〔(〔〕〔〕)〕
输出:
OK
var
a:string;
i,j:integer;
now:array[1..40]of char;
l:integer;
ans:integer;
top:integer;
begin
read(a);
l:=length(a);
ans:=0;
top:=0;
if (a[1]=')') or (a[1]=']') then
begin
ans:=1;
write('Wrong');
end;
while (a[i]' ') and (ans1) do
begin
if (a[i]='(') and (a[i]='[') then
begin
top:=top+1;
now[top]:=a[i];
end;
if (a[i]=')') then
if now[top]='(' then
top:=top-1
else
begin
write('wrong');
ans:=1;
end;
if (a[i]=']') then
if now[top]='[' then
top:=top-1
else
begin
write('Wrong');
ans:=1;
end;
i:=i+1;
end;
if ans=0 then write('OK');
end.
哪里错了?
▼优质解答
答案和解析
首先 建议用for循环写 for i:=1 to length(a) do
还有 在循环中输出‘Wrong’后最好 break;
致命之处在于 if语句 if (a[i]='(') and (a[i]='[') 应该是 if (a[i]='(') or (a[i]='[')
同时 第二个 ‘wrong’ 应是 ‘Wrong’
还有 在循环中输出‘Wrong’后最好 break;
致命之处在于 if语句 if (a[i]='(') and (a[i]='[') 应该是 if (a[i]='(') or (a[i]='[')
同时 第二个 ‘wrong’ 应是 ‘Wrong’
看了 pascal 括号配对问题假...的网友还看了以下:
中译英的一道题.在圣诞节的早晨.〔〕themorningofChristmasDay.括号里是填i 2020-04-06 …
1.墨为黑土〔〕2.衣不如新〔〕3.莺莺燕燕翠翠红红处处融融洽洽〔对联,此为上联,求下联.1.墨为 2020-04-12 …
x=2008,y=2006,求〔2x〔x^2y-xy^2〕+xy〔2xy-x^2〕〕÷x^2y的值 2020-05-13 …
〔〕思〔〕想〔括号里填思乡词语〕思乡成语 2020-05-21 …
〔〕温暖,〔〕防线,括号里面里面是动词 2020-06-26 …
问几个高数概念问题,全答对追加20一f(x)在〔a,b〕不连续,则f(x)在〔a,b〕不可积判断对 2020-07-21 …
雨水足以洗涤,足以滋润,足以〔〕在括号里填空雨水足以洗涤,足以滋润,足以〔〕在括号里填空 2020-12-20 …
赵括纸上谈兵赵括自少时学兵法,言兵事,以天下莫能当.尝与其父奢言兵事,奢不能难,然不谓善.括母问其故 2020-12-21 …
〔〕〔〕其谈,括号填什么 2021-01-04 …
昆曲《牡丹亭》中不同括号的用法昆曲《牡丹亭》中“”“〔〕”括起来的貌似都是曲牌?有什么区别吗? 2021-01-17 …