Fortran的528 470 481
它要求编译-fpp
标志(+3得分)1使用预处理指令(节省的方式比3个字符以上,所以我与完全没关系)。它也不区分大小写,所以有-8:D。通过不预处理保存5个字符endif
作为被使用一次呢。
要求文件具有.F90
扩展名(称其为hq9+.F90
),以便编译器强制进行预处理。该代码区分大小写;使其不区分大小写会添加大约16个字符,因此保存8个字符确实不值得。我以前的答案没有考虑改变复数的bottles
对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