FizzBuzzで頭の体操

C言語

#include <stdio.h>
#include <string.h>

int main(){
    int  cnt     = 100; 
    int  nums[]  = { 3    ,  5    };  
    char *strs[] = {"Fizz", "Buzz"};
    int  len     = sizeof(nums) / sizeof(nums[0]);

    for (int i = 1; i <= cnt; i++) {
        char res[256] = ""; 
        for (int j = 0; j < len; j++) {
            if (i%nums[j]==0) {
                strcat(res, strs[j]);
            }   
        }   
        if (strlen(res)==0) {
            sprintf(res, "%d", i); 
        }   
        printf("%s\n", res);
    }   
}

Java

public class FizzBuzz {
    public static void main(String args[]){
        int cnt       = 100;
        int[] nums    = {  3    ,  5   };
        String[] strs = { "Fizz", "Buzz" };
        int len       = nums.length;

        for (int i = 1; i <= cnt; i++) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < len; j++){
                if (i%nums[j]==0) {
                    sb.append(strs[j]);
                }   
            }   
            if (sb.length()==0) {
                sb.append(i);
            }   
            System.out.println(sb);
        }   
    }   
}

PHP

<?php
$cnt = 100;
$nums = array(  3     ,  5     );
$strs = array( "Fizz" , "Buzz" );
$len  = sizeof($nums);

for ($i = 1; $i <= $cnt; $i++) {
    $res = ""; 
    for ($j = 0; $j < $len; $j++) {
        if ($i%$nums[$j]==0) {
            $res .= $strs[$j];
        }   
    }   
    if ($res=="") {
        $res .= $i; 
    }   
    echo "$res\n";
}
?>

Perl

$cnt  = 100; 
@nums = (    3 ,     5 );
@strs = ("Fizz", "Buzz");
$len  = $#nums + 1;

for (my $i = 1; $i <= $cnt; $i++) {
    my $res = ""; 
    for (my $j = 0; $j < $len; $j++) {
        if (($i % $nums[$j]) == 0) {
            $res .= $strs[$j];
        }   
    }   
    if ($res eq ""){
         $res = $i; 
    }   
    printf("%s\n", $res);
}

Ruby

cnt  = 100
nums = [ 3    ,  5    ]   
strs = ["Fizz", "Buzz"]
len = nums.length

for i in 1..cnt
    res = ""
    for j in 0..len-1
        if i % nums[j] == 0
            res.concat(strs[j])
        end 
    end 
    if res == ""
        res = i.to_s
    end 
    print "#{res}\n"
end

Scala

object FizzBuzz {
    val cnt  = 100
    val nums = Array(  3    ,  5 )
    val strs = Array( "Fizz", "Buzz")
    val len  = nums.size

    def main(args:Array[String]){
        for (i <- 1 to cnt) {
            var res = ""
            for (j <- 0 to len - 1) {
                if (i % nums(j) == 0){ 
                    res += strs(j)
                }   
            }   
            if (res == "") {
                res = i.toString
            }   
            println(res)
        }   
    }   
}

Python

# coding: utf-8

nums = [3     ,  5    ]   
strs = ["Fizz", "Buzz"]

for i in range(100):
  i += 1
  res = ""
  for k,n in enumerate(nums):
    if (i % n) == 0:
      res += strs[k]
  print res if res != "" else i

トップ   差分 バックアップ リロード   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2014-11-25 (火) 01:48:09 (1881d)