Fortran 528 470  481
プリプロセッシングディレクティブを使用するには、-fppフラグ(+3でスコア)1でコンパイルする必要があります(3文字以上節約できるため、まったく問題ありません)。また、大文字と小文字が区別されないため、-8:Dがあります。とにかく一度使用されるものを前処理しないことで5文字を保存しましendifた。
コンパイラーが前処理を強制するように、ファイルに.F90拡張子を付ける必要があります(呼び出すのが理にかなっていますhq9+.F90)。コードでは大文字と小文字が区別されます。大文字と小文字を区別しないようにすると、16文字などが追加されるため、8文字を節約する価値はありません。私の以前の答えは、bottlesforの複数形の変化を説明していませんでした9。このバージョンはそれを修正します(悲しいことにもっと多くのキャラクターを追加します)。
#define P print*,
#define W " of beer on the wall"
#define N print'(x,i0,a,a)',
#define e enddo
#define S case
#define J len(trim(c))
character(len=99)::c,b=" bottles";read*,c;do i=1,J;if(all(c(i:i)/=["H","Q",'9',"+"])) then;P"Source code contains invalid characters";exit;endif;e;do i=1,J;select S(c(i:i));S("H");P"Hello, world!";S("Q");P c;S("9");l=8;do k=99,1,-1;N k,b(1:l),W;N k,b(1:l)," of beer";P "Take one down, pass it around";if(k==2)l=l-1;if(k==1)exit;N k-1,b(1:l),W;P"";e;P"No more",trim(b),W;S default;endselect;e;end
未処理および前処理なしの方がはるかによく見えます(おそらく、何が起こっているのかがわかるからです):
program hq9
   character(len=99)::c,b=" bottles"
   read*,c
   do i=1,len(trim(c))
! change the below to ["H","h","Q","q","9","+"] to be case-insensitive
      if(all(c(i:i)/=["H","Q","9","+"]))then
         print*,"Source code contains invalid characters"
         exit
      endif
   enddo
   do i=1,len(trim(c))
      select case(c(i:i))
        case("H")                ! change to case("H","h") for case-insensitive
           print*,"Hello, world!"
        case("Q")                ! change to case("Q","q") for case-insensitive
           print*, c
        case("9")
           l=8
           do k=99,1,-1
              print'(x,i0,a,a)', k,b(1:l)," of beer on the wall"
              print'(x,i0,a)', k,b(1:l)," of beer"
              print*,"Take one down, pass it around"
              if(k==2) l=l-1
              if(k==1) exit
              print'(x,i0,a)', k-1,b(1:l)," of beer on the wall"
              print*,""
           enddo
           print*,"No more",trim(b)," of beer on the wall"
        case default
           ! do nothing
      endselect
   enddo
end program hq9