3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

FizzBuzzAdvent Calendar 2017

Day 1

FreePascal で一番短い FizzBuzz

Last updated at Posted at 2017-11-30

FizzBuzz コード

追記 (2020/08/03)

Delphi 10.3 Rio から使えるインライン変数宣言で更に短く!
123 バイト

begin for var i:=1to 100do Writeln(TArray<Variant>.Create(i,'Fizz','Buzz','FizzBuzz')[Ord(i mod 3=0)+Ord(i mod 5=0)*2])end.

追記 (12/7 14:00)

より短いコードができてしまいました…!
134 バイト

uses Variants;var i:Word;begin for i:=1to 100do Writeln(VarArrayOf([i,'Fizz','Buzz','FizzBuzz'])[Ord(i mod 3=0)+Ord(i mod 5=0)*2])end.

Ideone での実行はこちらから

一番短いコード

恐らくこれ以上短くすることは出来ないと思います。
138 バイト!

var i:Word;S:Variant;begin for i:=1to 100do begin S:=Copy('Fizz',i mod 3*9,4)+Copy('Buzz',i mod 5*9,4);if S=''then S:=i;Writeln(S)end;end.

Ideone での実行はこちらから

清書

var 
  i: Word;
  S: Variant;
begin
  for i := 1 to 100 do 
  begin
    S := Copy('Fizz', i mod 3 * 9, 4) + Copy('Buzz', i mod 5 * 9, 4);
    if S = '' then 
      S:=i;

    Writeln(S)
  end;
end.

特におかしな書き方をしたわけではなく、ちょっと特殊なのは Variant を使ったくらい。

まとめ

結局、Pascal の場合、めちゃくちゃ厳格だから普通に書くのが一番短いのだ…

3
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?