题解:

傻逼模拟题

果断的复制了题解(还没有c++题解?)

代码:

program p2509;
type arr=array[1..26] of boolean;
var tot:longint;
    s:array[1..100] of ansistring;
    b:arr;
procedure init;
begin
  tot:=0;
  while not eof do
  begin
    inc(tot);
    readln(s[tot]);
  end;
end;
procedure warn1(n:longint;c:char);
begin
  writeln('Line ',n,': variable ',c,' might not have been initialized');
end;
procedure warn2(n:longint);
begin
  writeln('Line ',n,': unreachable code');
end;
function judge(str:ansistring):longint;
begin
  if str[2]='F' then
    exit(3)
  else if str[2]='E' then
    exit(2)
  else if str[2]='L' then
    exit(4)
  else if str[2]='N' then
    exit(5)
  else
    exit(1);
end;
function dfs(var k:longint;var c:arr):boolean;
var d,e,c1,c2:arr;
    str:ansistring;
    i,j,sum:longint;
    a:array[1..26] of longint;
    b1,b2:boolean;
begin
  d:=c;
  while k<=tot do
  begin
    str:=s[k];
    if judge(str)=1 then                                                                                                 //赋值语句
    begin
      fillchar(a,sizeof(a),0);
      for i:=5 to length(str) do
        if (str[i]>='A') and (str[i]<='Z') and not c[ord(str[i])-64] then
          inc(a[ord(str[i])-64]);
      for i:=1 to 26 do
        if a[i]>0 then
          warn1(k,chr(i+64));
      c[ord(str[1])-64]:=true;
    end
    else if judge(str)=2 then                                                                                        //return语句
    begin
      fillchar(a,sizeof(a),0);
      for i:=8 to length(str) do
        if (str[i]>='A') and (str[i]<='Z') and not c[ord(str[i])-64] then
          inc(a[ord(str[i])-64]);
      for i:=1 to 26 do
        if a[i]>0 then
          warn1(k,chr(i+64));
      sum:=0;
      j:=k;
      inc(k);
      while k<=tot do
      begin
        str:=s[k];
        if judge(str)=3 then inc(sum);
        if sum=0 then
        if (str[2]='L') or (str[2]='N') then
        begin
          for i:=j+1 to k-1 do
            if judge(s[i])<=3 then warn2(i);
          c:=d;
          exit(true);
        end;
      if str[2]='N' then dec(sum);
      inc(k);
    end;
    if k>tot then
      for i:=j+1 to tot do
        if judge(s[i])<=3 then warn2(i);
    end
    else if judge(str)=3 then                                                                                      //if语句
    begin
      j:=3;
      while copy(str,j,4)<>'THEN' do
        inc(j);
      fillchar(a,sizeof(a),0);
      for i:=3 to j-1 do
        if (str[i]>='A') and (str[i]<='Z') and not c[ord(str[i])-64] then
          inc(a[ord(str[i])-64]);
      for i:=1 to 26 do
        if a[i]>0 then
          warn1(k,chr(i+64));
      e:=c;
      inc(k);
      b1:=dfs(k,c);
      c1:=c;
      c:=e;
      if judge(s[k])=4 then
      begin
        inc(k);
        b2:=dfs(k,c);
        c2:=c;
        c:=e;
        if b1 and not b2 then
          c:=c2
        else if not b1 and b2 then
          c:=c1
        else if not b1 and not b2 then
        begin
          for i:=1 to 26 do
            if c1[i] and c2[i] then c[i]:=true;
        end;
        b1:=b1 and b2;
      end
      else
        b1:=false;
      if b1 then
      begin
        sum:=0;
        j:=k;
        inc(k);
        while k<=tot do
        begin
          str:=s[k];
          if judge(str)=3 then inc(sum);
          if sum=0 then
          if (str[2]='L') or (str[2]='N') then
          begin
            for i:=j+1 to k-1 do
              if judge(s[i])<=3 then warn2(i);
            c:=d;
            exit(true);
          end;
          if str[2]='N' then dec(sum);
          inc(k);
        end;
        if k>tot then
          for i:=j+1 to tot do
            if judge(s[i])<=3 then warn2(i);
      end;
    end
    else if judge(str)=4 then                                                                             //else语句
    begin
      exit(false);
    end
    else if judge(str)=5 then                                                                              //end if语句
    begin
      exit(false);
    end;
    inc(k);
  end;
end;
procedure main;
var i,l:longint;
    bool:boolean;
begin
  l:=length(s[1]);
  fillchar(b,sizeof(b),false);
  for i:=7 to l do
    if (s[1][i]>='A') and (s[1][i]<='Z') then
      b[ord(s[1][i])-64]:=true;
  i:=2;
  bool:=dfs(i,b);
end;
begin
  init;
  main;
end.

 

posted on 2018-02-26 15:00  宣毅鸣  阅读(92)  评论(0编辑  收藏  举报