题解 P4908 【小奔上课】
ShineEternal · · 题解
其他相关题目,请到这里
我来一发标程+简要解析,不一定最优,欢迎来讨论。
解析:这一题只有两个考点:
1.字符串计算(改成四则运算已经很贴心了QvQ)
2.残缺位置的判断
(题目的说明中已经提到一些,注释中也会再次说明)
var
ss:string;
number:array[0..100] of real;
a:array[1..10] of longint;
symbol:array[1..100] of char;
p,i,j,m:longint;
max,min:real;
b:boolean;
function check0(q:longint;s:string):boolean;
begin
check0:=false;
if (q=1)and((s[q+1]>='0')and(s[q+1]<='9')) then exit;
if (q=1) then begin check0:=true; exit; end;
//数字首尾不能是零,否则算式首位都能放0
if (q=length(s)) then
begin
check0:=true;
exit;
end;
//最后一位只要能放数字就能放零
if (((s[q-1]>'9')or(s[q-1]<'0'))and(s[q-1]<>'('))and((s[q+1]>='0')and(s[q+1]<='9')) then exit;
check0:=true;
//其他情况都可以
end;
function checkf(q:longint;s:string):boolean;
begin //检查是否能放符号
checkf:=false;
if (q=length(s))or(q=1) then exit;
//首尾不能放
if (s[q-1]='(')or(s[q+1]=')') then exit;
//右括号前左括号后不能放
if ((s[q+1]>'9')or(s[q+1]<'0'))and(s[q+1]<>'*')and(s[q+1]<>'-')and(s[q+1]<>'(') then exit;
//符号之前不能放,但是左括号、负号和残缺之前能放
if ((s[q-1]>'9')or(s[q-1]<'0'))and(s[q-1]<>')') then exit;
//除了右括号,符号后面都不能放
checkf:=true;
end;
function checks(q:longint;s:string):boolean;
var
i,m,n:longint;
b:boolean;
begin //检查是否能放数
m:=0;
for i:=1 to q do
begin
if s[i]='(' then inc(m);
if s[i]=')' then dec(m);
end; //检查是否在括号内
if m=0 then //如果不在括号内
begin
checks:=true;
b:=false;
for i:=1 to length(s) do if ((s[i]='*')and(i<>q))or(s[i]='/')or((s[i]='-')and((i<>1)and(s[i-1]>='0')and(s[i-1]<='9')))or(s[i]='+') then b:=true;
if (b=false) then begin checks:=false; exit; end;
//如果算式全是数字,那么这里就不能放数字(如:649*,这样形成不了算式)
if (q=1)or(q=length(s)) then exit;
//首尾都能放
if (s[q-1]=')')or(s[q+1]='(') then begin checks:=false; exit; end;
//右括号后面,左括号前面都不能放
checks:=true;
exit;
end else
begin
m:=q;
n:=m;
while true do
begin
inc(m);
dec(n);
if (s[n]='(')or(s[n]=')')or(s[m]='(')or(s[m]=')') then break;
end;
//找出最近的一层括号
if (s[n]='(')or(s[n]=')') then begin
i:=n+1;
while (s[i]<>'(')and(s[i]<>')') do
begin
if ((s[i]='*')and(i<>q))or(s[i]='/')or((s[i]='-')and((s[i-1]>='0')and(s[i-1]<='9')))or(s[i]='+') then
begin
checks:=true;
if (q=1)or(q=length(s)) then exit;
if (s[q-1]=')')or(s[q+1]='(') then begin checks:=false; exit; end;
checks:=true;
exit;
end;
inc(i);
end;
checks:=false;
exit;
end;
if (s[m]='(')or(s[m]=')') then begin
i:=m-1;
while (s[i]<>'(')and(s[i]<>')') do
begin
if ((s[i]='*')and(i<>q))or(s[i]='/')or((s[i]='-')and((s[i-1]>='0')and(s[i-1]<='9')))or(s[i]='+') then
begin
checks:=true;
if (q=1)or(q=length(s)) then exit;
if (s[q-1]=')')or(s[q+1]='(') then begin checks:=false; exit; end;
//这里同上,将括号内看成一个算式来判断
checks:=true;
exit;
end;
dec(i);
end;
checks:=false;
exit;
end;
end;
end;
procedure push(s:string);
begin
inc(p);
symbol[p]:=s[i];
end;
procedure pop(s:string);
begin
dec(p);
case symbol[p+1] of
'+':number[p]:=number[p]+number[p+1];
'-':number[p]:=number[p]-number[p+1];
'*':number[p]:=number[p]*number[p+1];
'/':begin
if number[p+1]=0 then begin b:=true; exit; end;
number[p]:=number[p]/number[p+1];
end;
end;
end;
function can(s:string):boolean;
begin
can:=true;
if (s[i] in ['+','-'])and (symbol[p]<>'(') then exit;
if (s[i] in ['*','/'])and (symbol[p] in ['*','/']) then exit;
can:=false;
end;
function js(s:string):real;
var
t:string;
j,code:longint;
begin
s:='('+s+')';
i:=1;
p:=0;
while i<=length(s) do
begin
while s[i]='(' do
begin
push(s);
inc(i);
end;
j:=i;
repeat
inc(i);
until (s[i]<'0')or (s[i]>'9');
t:=copy(s,j,i-j);
val(t,number[p],code);
repeat
if s[i]=')' then
begin
while symbol[p]<>'(' do pop(s);
dec(p);
number[p]:=number[p+1];
end else
begin
while can(s) do
begin
pop(s);
if b then begin
js:=min+(max-min)/2;
b:=false;
exit;
end;
end;
push(s);
end;
inc(i);
until (i>length(s)) or (s[i-1]<>')');
end;
js:=number[0];
end;
//通过栈实现计算,这里自动排除了除以0的情况
procedure zx(o:longint; st:string);
var
i:real;
begin
if o>m then begin
i:=js(st);
if i>max then max:=i;
if i<min then min:=i;
exit;
//求最大值和最小值
end;
if checks(a[o],st) then begin
if check0(a[o],st) then
begin
st[a[o]]:='0';
zx(o+1,st);
end;
st[a[o]]:='1';
zx(o+1,st);
st[a[o]]:='9';
zx(o+1,st);
//能放23456就能放19,并且19得出的值比23456等数都大
end;
if (a[o]=length(st))or((st[a[o]-1]='-')and(st[a[o]-2]='-'))or(st[a[o]+1]=')') then
i:=0
//这里除了极端条件(如:--*)不能放-外,都能放-
else
begin
st[a[o]]:='-';
zx(o+1,st);
end;
if checkf(a[o],st) then begin
st[a[o]]:='*';
zx(o+1,st);
st[a[o]]:='+';
zx(o+1,st);
st[a[o]]:='/';
zx(o+1,st);
end;
end;
begin
readln(ss);
max:=-maxlongint-1;
min:=maxlongint;
for j:=1 to length(ss) do if ss[j]='*' then
begin
inc(m);
a[m]:=j;
//记录每一个残缺位置
end;
if m=0 then
begin
writeln(js(ss));
halt;
end;
zx(1,ss);
max:=(max+min)/2;
writeln(max:0:2);
end.
//完结撒花