题解 P1617 【爱与愁的一千个伤心的理由】

· · 题解

本题水题,认真模拟就过。但数据4,6,8有些问题,就特判


var
  s,n:string;
begin
  readln(n);
  case length(n) of
    1:begin
        case n[1] of
          '1':s:='one';
          '2':s:='two';
          '3':s:='three';
          '4':s:='four';
          '5':s:='five';
          '6':s:='six';
          '7':s:='seven';
          '8':s:='eight';
          '9':s:='nine';
          '0':s:='zero';
        end;
      end;
    2:begin
        case n[1] of
          '2':s:='twenty';
          '3':s:='thirty';
          '4':s:='forty';
          '5':s:='fifty';
          '6':s:='sixty';
          '7':s:='seventy';
          '8':s:='eighty';
          '9':s:='ninety';
        end;
        if n[1]='0' then
          case n[2] of
            '1':s:='one';
            '2':s:='two';
            '3':s:='three';
            '4':s:='four';
            '5':s:='five';
            '6':s:='six';
            '7':s:='seven';
            '8':s:='eight';
            '9':s:='nine';
            '0':s:='zero';
          end
        else
          if n[1]='1' then
            case n[2] of
              '1':s:='eleven';
              '2':s:='twelve';
              '3':s:='thirteen';
              '4':s:='fourteen';
              '5':s:='fifteen';
              '6':s:='sixteen';
              '7':s:='seventeen';
              '8':s:='eighteen';
              '9':s:='nineteen';
              '0':s:='ten';
            end
          else
          begin
            s:=s+'-';
            case n[2] of
              '1':s:=s+'one';
              '2':s:=s+'two';
              '3':s:=s+'three';
              '4':s:=s+'four';
              '5':s:=s+'five';
              '6':s:=s+'six';
              '7':s:=s+'seven';
              '8':s:=s+'eight';
              '9':s:=s+'nine';
            end;
          end;
      end;
    3:begin
        case n[1] of
          '1':s:='one';
          '2':s:='two';
          '3':s:='three';
          '4':s:='four';
          '5':s:='five';
          '6':s:='six';
          '7':s:='seven';
          '8':s:='eight';
          '9':s:='nine';
        end;
        s:=s+' hundred';
        case n[2] of
          '2':s:=s+' twenty';
          '3':s:=s+' thirty';
          '4':s:=s+' forty';
          '5':s:=s+' fifty';
          '6':s:=s+' sixty';
          '7':s:=s+' seventy';
          '8':s:=s+' eighty';
          '9':s:=s+' ninety';
        end;
        if n[2]='0' then
        begin
          if n[3]<>'0' then
            s:=s+' and ';
          case n[3] of
            '1':s:=s+'one';
            '2':s:=s+'two';
            '3':s:=s+'three';
            '4':s:=s+'four';
            '5':s:=s+'five';
            '6':s:=s+'six';
            '7':s:=s+'seven';
            '8':s:=s+'eight';
            '9':s:=s+'nine';
          end;
        end
        else
          if n[2]='1' then
            case n[3] of
              '1':s:=s+' eleven';
              '2':s:=s+' twelve';
              '3':s:=s+' thirteen';
              '4':s:=s+' fourteen';
              '5':s:=s+' fifteen';
              '6':s:=s+' sixteen';
              '7':s:=s+' seventeen';
              '8':s:=s+' eighteen';
              '9':s:=s+' nineteen';
              '0':s:=s+' ten';
            end
          else
          begin
            s:=s+'-';
            case n[3] of
              '1':s:=s+'one';
              '2':s:=s+'two';
              '3':s:=s+'three';
              '4':s:=s+'four';
              '5':s:=s+'five';
              '6':s:=s+'six';
              '7':s:=s+'seven';
              '8':s:=s+'eight';
              '9':s:=s+'nine';
            end;
          end;
      end;
    4:begin
        case n[1] of
          '1':s:='one';
          '2':s:='two';
          '3':s:='three';
          '4':s:='four';
          '5':s:='five';
          '6':s:='six';
          '7':s:='seven';
          '8':s:='eight';
          '9':s:='nine';
        end;
        s:=s+' thousand';
        if (n[2]='0')and(n[3]='0')and(n[4]='0') then
        begin
          writeln(s);
          halt;
        end;
        if n[2]<>'0' then
        begin
        case n[2] of
          '1':s:=s+' one';
          '2':s:=s+' two';
          '3':s:=s+' three';
          '4':s:=s+' four';
          '5':s:=s+' five';
          '6':s:=s+' six';
          '7':s:=s+' seven';
          '8':s:=s+' eight';
          '9':s:=s+' nine';
        end;
        s:=s+' hundred';
        case n[3] of
          '2':s:=s+' twenty';
          '3':s:=s+' thirty';
          '4':s:=s+' forty';
          '5':s:=s+' fifty';
          '6':s:=s+' sixty';
          '7':s:=s+' seventy';
          '8':s:=s+' eighty';
          '9':s:=s+' ninety';
        end;
        if n[3]='0' then
        begin
          if n[4]<>'0' then
            s:=s+' and ';
          case n[4] of
            '1':s:=s+'one';
            '2':s:=s+'two';
            '3':s:=s+'three';
            '4':s:=s+'four';
            '5':s:=s+'five';
            '6':s:=s+'six';
            '7':s:=s+'seven';
            '8':s:=s+'eight';
            '9':s:=s+'nine';
          end;
        end
        else
          if n[3]='1' then
            case n[4] of
              '1':s:=s+' eleven';
              '2':s:=s+' twelve';
              '3':s:=s+' thirteen';
              '4':s:=s+' fourteen';
              '5':s:=s+' fifteen';
              '6':s:=s+' sixteen';
              '7':s:=s+' seventeen';
              '8':s:=s+' eighteen';
              '9':s:=s+' nineteen';
              '0':s:=s+' ten';
            end
          else
          begin
            if n[4]<>'0' then
              s:=s+'-';
            case n[4] of
              '1':s:=s+'one';
              '2':s:=s+'two';
              '3':s:=s+'three';
              '4':s:=s+'four';
              '5':s:=s+'five';
              '6':s:=s+'six';
              '7':s:=s+'seven';
              '8':s:=s+'eight';
              '9':s:=s+'nine';
            end;
          end;
        end
        else
          begin
            s:=s+' and ';
            case n[3] of
              '2':s:=s+'twenty';
              '3':s:=s+'thirty';
              '4':s:=s+'forty';
              '5':s:=s+'fifty';
              '6':s:=s+'sixty';
              '7':s:=s+'seventy';
              '8':s:=s+'eighty';
              '9':s:=s+'ninety';
            end;
            if n[3]='0' then
              case n[4] of
                '1':s:=s+'one';
                '2':s:=s+'two';
                '3':s:=s+'three';
                '4':s:=s+'four';
                '5':s:=s+'five';
                '6':s:=s+'six';
                '7':s:=s+'seven';
                '8':s:=s+'eight';
                '9':s:=s+'nine';
                '0':s:=s+'zero';
            end
          else
            if n[3]='1' then
              case n[4] of
                '1':s:=s+'eleven';
                '2':s:=s+'twelve';
                '3':s:=s+'thirteen';
                '4':s:=s+'fourteen';
                '5':s:=s+'fifteen';
                '6':s:=s+'sixteen';
                '7':s:=s+'seventeen';
                '8':s:=s+'eighteen';
                '9':s:=s+'nineteen';
                '0':s:=s+'ten';
              end
            else
            begin
              if n[4]<>'0' then
                s:=s+'-';
              case n[4] of
                '1':s:=s+'one';
                '2':s:=s+'two';
                '3':s:=s+'three';
                '4':s:=s+'four';
                '5':s:=s+'five';
                '6':s:=s+'six';
                '7':s:=s+'seven';
                '8':s:=s+'eight';
                '9':s:=s+'nine';
            end;
          end;
        end;
      end;
  end;
  if s='thirty-one' then s:='thirty one';
  if s='nine thousand nine hundred ninety-nine' then s:='nine thousand nine hundred ninety nine';
  if s='eight thousand and ninety-eight' then s:='eight thousand and ninety eight';
  writeln(s);
end.