bardakov_a_a's blog

By bardakov_a_a, 12 years ago, In Russian

Дамы и Господа! Нужна ваша помощь. Вот задача из самого первого соревнования codeforces http://codeforces.com/contest/1/problem/B?locale=ru

Помогите найти ошибку в логике. Валится на 7 тесте

program Project2;

{$APPTYPE CONSOLE}

var
  n: Integer;
  i: Integer;
  tmp: String;


function Selection(tmp: String): Boolean;
var i,j: Integer;
begin
  Selection:=True;
  if tmp[1]='R' then
  begin
    Delete(tmp, 1, 2);
    if Pos('C', tmp)<>0 then
        Selection:=False;
  end;
end;

function Power(a, n: Integer): Int64;
begin
  if n=0 then Power:=1
  else if odd(n) then Power:=Power(a*a, n div 2)*a
       else Power:=Power(a*a, n div 2);
end;

procedure ToTwoView(tmp: String);
var i, j: Integer;
  k: Integer;
  st: String;
  sum: Int64;
begin
  sum:=0;
  j:=Length(tmp);
  for i := j downto 1 do
    if tmp[i] In ['0'..'9'] then
      st:= tmp[i] + st
    else break;
  for j := i downto 1 do
    sum:=sum + (Ord(tmp[j])-Ord('A')+1)*Power(26,i-j);
  WriteLn('R',st,'C',sum);
end;

procedure ToOneView(tmp: String);
var st, st2: String;
  i,j,k: Integer;
  p, code: Integer;
begin
  i:=Pos('C', tmp);
  st:='';
  k:=Length(tmp);
  for j := 1 to i do
    if tmp[j] In ['0'..'9'] then
      st:=st+tmp[j];
  for j := i to k do
    if tmp[j] In ['0'..'9'] then
      st2:=st2+tmp[j];
  Val(st2, p, code);
  st2:='';
  while p>0 do
  begin
    i:= p mod 26;
    if i=0 then i:=26;
    st2:=st2+Chr(Ord('A')-1+i);
    p:=p-i;
    p:=p div 26;
  end;
  k:=Length(st2);
  for i := k downto 1 do
    Write(st2[i]);
  Write(st);
  WriteLn;
end;

begin
  ReadLn(n);
  for i := 1 to n do
  begin
    ReadLn(tmp);
    case Selection(tmp) of
      true: ToTwoView(tmp);
      false: ToOneView(tmp);
    end;
  end;
end.

Думаю, функция selection при каких-то значениях не верно определяет, но конкретного примера пока не нашел.

  • Vote: I like it
  • +1
  • Vote: I do not like it