2017.6.4 入门组 NO.4——猜数

这里写图片描述
这里写图片描述


这题就是求一个区间[l..r],那开始将l=-10000 r=10000
每次输入一次判断,如果为”N”,那么就将符号反转,如 >→<= <→>= >=→< <=→>
那么符号处理好后,
①如果为”>”或”>=”,那么如果当x大于l且小于或等于r,则l=x
如果当x大于r,则输出”Impossible”
②如果为”<”或”<=”,那么如果当x大于或等于l且小于r,则r=x
如果当x小于l,则输出”Impossible”
(Tips:每当枚举”>”或”<”,x+1)
有此公式,妈妈再也不用担心我不能AC了


代码如下:

var s:string;
    l,r,n,i:longint;

procedure doit;
var  l2,l1,fw:longint;
     fh,s1:string;
begin
  l2:=pos(' ',s); fh:=copy(s,1,l2-1); s[l2]:=',';
  l1:=pos(' ',s);
  s1:=copy(s,l2+1,l1-l2-1);
  val(s1,fw);
  s[l1]:=',';
  if s[l1+1]='N' then
    if fh='>' then fh:='<='
    else if fh='<' then fh:='>='
         else if fh='>=' then fh:='<'
              else if fh='<=' then fh:='>';
  if (fh='>')or(fh='>=') then
      begin
        if fh='>' then fw:=fw+1;
        if (fw>l)and(fw<=r) then l:=fw
        else if fw>r then begin writeln('Impossible'); close(input); close(output); halt; end;
      end;
  if (fh='<')or(fh='<=') then
      begin
        if fh='<' then fw:=fw-1;
        if (fw<r)and(fw>=l) then r:=fw
        else if (fw<l) then begin writeln('Impossible'); close(input); close(output); halt; end;
      end;
end;

begin
  readln(n);
  l:=-10000; r:=10000;
  for i:=1 to n do
    begin
      readln(s);
      doit;
    end;
  writeln(l);
end.
原文地址:https://www.cnblogs.com/Comfortable/p/8412283.html