Programming Riddle: How might you translate an Excel column name to a number?

前端 未结 28 1335
[愿得一人]
[愿得一人] 2020-11-29 22:26

I was recently asked in a job interview to resolve a programming puzzle that I thought it would be interesting to share. It\'s about translating Excel column letters to actu

28条回答
  •  北海茫月
    2020-11-29 23:00

    Delphi:

    // convert EXcel column name to column number 1..256
    // case-sensitive; returns 0 for illegal column name
    function cmColmAlfaToNumb( const qSRC : string ) : integer;
    var II : integer;
    begin
       result := 0;
       for II := 1 to length(qSRC) do begin
          if (qSRC[II]<'A')or(qSRC[II]>'Z') then begin
             result := 0;
             exit;
          end;
          result := result*26+ord(qSRC[II])-ord('A')+1;
       end;
       if result>256 then result := 0;
    end;
    

    -Al.

提交回复
热议问题