public class FizzBuzz { public static class NumMessagePair { private final int base; private final String message; public NumMessagePair(int base, String message) { this.base = base; this.message = message; } public boolean isAliquot(int num) { return (num / base * base == num); } public String getMessage() { return message; } } private final int start; private final int last; private final NumMessagePair[] pairs; private final StringBuilder result = new StringBuilder(); public FizzBuzz(int start, int last, NumMessagePair[] pairs) { this.start = start; this.last = last; this.pairs = pairs; } public static void main(String[] args) { System.out.print(new FizzBuzz(1, 100, new NumMessagePair[] { new NumMessagePair(3, "Fizz"), new NumMessagePair(5, "Buzz") }) .execute()); } public String execute() { for (int i = 1; i <= last; i++) { if (isAnyAliquot(i)) { addMessages(i); } else { result.append(i); } result.append("\n"); } return result.toString(); } private void addMessages(int num) { for (int i = 0; i < pairs.length; i++) { if (pairs[i].isAliquot(num)) { result.append(pairs[i].getMessage()); } } } private boolean isAnyAliquot(int num) { for (int i = 0; i < pairs.length; i++) { if (pairs[i].isAliquot(num)) { return true; } } return false; } }
http://anond.hatelabo.jp/20070508170219
#include <stdio.h> int main(void); int main(void) { int cn,nf,nb,nfb; cn=nf=nb=nfb=0; for(cn=1;cn<=100;cn++){ while(cn>nf) nf+=3; while(cn>nb) nb+=5; while(cn>nfb) nfb+=15; if(cn==nfb){ printf("FizzBuzz\n"); }else if(cn==nf){ printf("Fizz\n"); }else if(cn==nb){ printf("Buzz\n"); }else{ printf("%d\n",cn); } } return(0); }
http://anond.hatelabo.jp/20070508170219
Public Sub FizzBuzz() Dim i As Integer Dim cc As Range For i = 1 To 100 Set cc = Cells(i, 1) cc.FormulaR1C1 = _ "=IF(INT(ROW()/3)*3=ROW()*1,IF(INT(ROW()/5)*5=ROW()*1,""FizzBuzz"",""Fizz""),IF(INT(ROW()/5)*5=ROW()*1,""Buzz"",ROW()))" Next i Set cc = Nothing End Sub
printはワークシート上に。
個人的にはもっと変わり種が見てみたい。冴子先生にFizzBuzzしゃべらせてみるとか。
バグ? マジで? 自称上級プログラマーなのに! やっぱテスト書かなきゃだめだね。
あ、あと変態は間違いない。
public class FizzBuzz { private final int start; private final int last; private final int fizz; private final int buzz; public FizzBuzz(int start, int last, int fizz, int buzz) { this.start = start; this.last = last; this.fizz = fizz; this.buzz = buzz; } public static void main(String[] args) { new FizzBuzz(1, 100, 3, 5).execute(); } private void execute() { for (int i = 1; i <= last; i++) { System.out.println((i) + (isMultiple(i, fizz) ? "Fizz" : "") + (isMultiple(i, buzz) ? "Buzz" : "")); } } private boolean isMultiple(int i, int base) { for (int j = base; j <= last; j += base) { if (i == j) { return true; } } return false; } }