特に技術を紹介するわけではないがメモとして作成。
まずは普通に
fb1.cs
using System;
namespace FizzBuzz
{
public class Program
{
public static void Main(string[] args)
{
for (var i = 1; i <= 100; ++i)
{
if (i % 3 == 0)
Console.Write("Fizz");
if (i % 5 == 0)
Console.Write("Buzz");
if (Console.CursorLeft == 0)
Console.Write(i);
Console.WriteLine();
}
Console.ReadKey();
}
}
}
2
fb2.cs
using System;
namespace FizzBuzz
{
public class Program
{
public static void Main(string[] args)
{
for (var i = 1; i <= 100; ++i)
{
string s;
Console.WriteLine(string.IsNullOrEmpty(s = (i % 3 == 0 ? "Fizz" : "") + (i % 5 == 0 ? "Buzz" : "")) ? i.ToString() : s);
}
}
}
}
Enumerable.Range -> LINQ
fb3.cs
using System;
using System.Linq;
namespace FizzBuzz
{
public class Program
{
public static void Main(string[] args)
{
Enumerable.Range(1, 100)
.Select(n => (n, new[] { (3, "Fizz"), (5, "Buzz") }
.Where(t => n % t.Item1 == 0).Select(t => t.Item2))
).Select(t => t.Item2.Any() ? string.Join("", t.Item2) : t.Item1.ToString())
.ToList().ForEach(Console.WriteLine);
}
}
}
Dictionary, 再帰, 参照渡し(特に意味はない)
fb4.cs
using System;
using System.Collections.Generic;
using System.Linq;
namespace FizzBuzz
{
public class Program
{
public static void Main(string[] args)
{
var dict = new Dictionary<int, string>();
for(var i = 1; i <= 100; ++i)
{
if (i % 3 == 0)
dict[i] = "Fizz";
if (i % 5 == 0)
dict[i] = string.Concat(dict.ContainsKey(i) ? dict[i] : "", "Buzz");
}
DoFizzBuzz(dict, 1, 100);
}
private static void DoFizzBuzz(in Dictionary<int, string> dict, int n, int end)
{
Console.WriteLine(dict.ContainsKey(n) ? dict[n] : n.ToString());
if(n < end)
DoFizzBuzz(dict, n + 1, end);
}
}
}
Task(動作不安定)
fb5.cs
using System;
using System.Threading.Tasks;
namespace FizzBuzz
{
public class Program
{
private static bool Complete { set; get; }
public static void Main(string[] args)
{
DoFizzBuzz();
}
private static async void DoFizzBuzz()
{
RunTerminate();
RunN(); await Task.Delay(250);
RunFizz(); await Task.Delay(250);
RunBuzz(); await Task.Delay(250);
RunFizzBuzz();
}
private static async void RunTerminate()
{
await Task.Delay(1000 * 101);
Complete = true;
}
private static async void RunFizzBuzz()
{
while (!Complete)
{
if (Console.CursorTop % 15 == 0)
{
Console.CursorTop--;
Console.WriteLine("FizzBuzz");
}
await Task.Delay(1000);
}
}
private static async void RunBuzz()
{
while (!Complete)
{
if (Console.CursorTop % 5 == 0)
{
Console.CursorTop--;
Console.WriteLine("Buzz");
}
await Task.Delay(1000);
}
}
private static async void RunFizz()
{
while (!Complete)
{
if(Console.CursorTop % 3 == 0)
{
Console.CursorTop--;
Console.WriteLine("Fizz");
}
await Task.Delay(1000);
}
}
private static async void RunN()
{
while (!Complete)
{
Console.WriteLine(Console.CursorTop + 1);
await Task.Delay(1000);
}
}
}
}
短く
fb6.cs
class P{static void Main(){for(var i=0;i++<100;)System.Console.WriteLine((i%3<1?"Fizz":"")+(i%5<1?"Buzz":i%3<1?"":""+i));}}
C♯7.2の機能を盛る
fb7.cs
using System;
namespace FizzBuzz
{
public class Program
{
public static void Main(string[] args)
{
var a = 0b_011;
var b = 0b_101;
for (var i = 1; i <= 100; ++i)
Console.WriteLine(string.IsNullOrEmpty(PrintString(i, b3: a, b5: b, out var v)) ? i.ToString() : v);
}
private protected static ref readonly string PrintString(in int i, in int b3, in int b5, out string s)
{
s = (i % b3 == 0 ? "Fizz" : "") + (i % b5 == 0 ? "Buzz" : "");
return ref s;
}
}
}