変形Fizz-Buzz問題
Posted feedbacks - C#
どうでしょうか?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | static void Main(string[] args)
{
string str = string.Empty;
for (int i = 1; i <= 20; ++i)
{
Console.Write("{0,2}:", i );
str = "hoge";
if (i % 15 == 0)
{
str = "FizzBuzz";
}
if (i % 5 == 0 && str == "hoge")
{
str = "Buzz";
}
if (i % 3 == 0 && str == "hoge")
{
str = "Fizz";
}
Console.WriteLine(str);
}
}
|
出力が分かれてたので修正。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | static void Main(string[] args)
{
string str = string.Empty;
for (int i = 1; i <= 20; ++i)
{
str = "hoge";
if (i % 15 == 0)
{
str = "FizzBuzz";
}
if (i % 5 == 0 && str == "hoge")
{
str = "Buzz";
}
if (i % 3 == 0 && str == "hoge")
{
str = "Fizz";
}
Console.WriteLine("{0,2}:{1}", i, str);
}
}
|
こんな感じでOKでしょうか。
C#2.0 の ?? 演算子使いましたけど、これは else には該当……するのかなしないのかな。
1 2 3 4 5 6 7 8 9 | using System;
static class FizzBuzz {
public static void Main(String[] args) {
string[] msg = new string[15];
for(int i = 2; i <= msg.Length; i += 3) msg[i] += "Fizz";
for(int i = 4; i <= msg.Length; i += 5) msg[i] += "Buzz";
for(int i = 0; i < 20; i++) Console.WriteLine("{0,2}:{1}", i + 1, msg[i%msg.Length] ?? "hoge");
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | using System;
class FizzBuzz
{
static void Main(string[] args)
{
string[] fizzbuzz = { "hoge", "Fizz", "Buzz", "FizzBuzz" };
for (int i = 1; i <= 20; i++)
{
int index = 0;
if (i % 3 == 0) index += 1;
if (i % 5 == 0) index += 2;
Console.WriteLine("{0,2} : {1}", i, fizzbuzz[index]);
}
}
}
|
無限にyield。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | using System;
using System.Collections.Generic;
class Program {
static IEnumerable<string> FizzBuzz() {
while (true) {
yield return "hoge";
yield return "hoge";
yield return "Fizz";
yield return "hoge";
yield return "Buzz";
yield return "Fizz";
yield return "hoge";
yield return "hoge";
yield return "Fizz";
yield return "Buzz";
yield return "hoge";
yield return "Fizz";
yield return "hoge";
yield return "hoge";
yield return "FizzBuzz";
}
}
static void Main(string[] args) {
int i = 0;
foreach (string s in FizzBuzz()) {
if (++i > 20) break;
Console.WriteLine("{0, 2}:{1}", i, s);
}
}
}
|
C#3.0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public void Test()
{
Func<Func<string, string>, Func<string, string>> f = ( fn => fn );
int i = 1;
var f_hoge = f( x => {
if ( string.IsNullOrEmpty( x ) ) return "hoge";
return x;
} );
var f_5 = f( x => {
if ( i%5 == 0 ) return f_hoge( x + "Buzz" );
return f_hoge( x );
} );
Func<string> f_3 = ( () => {
if ( i%3 == 0 ) return f_5( "Fizz" );
return f_5( "" );
} );
for ( ; i <= 20 ; i++ )
Console.WriteLine( "{0,2}:{1}", i, f_3() );
}
|
Mainメソッドが一行になるように書いてみました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | using System;
using System.Linq;
class Program
{
static void Main(string[] args)
{
Enumerable.Range(1, 20).Select(n => new FizzBuzz(n)).ToList().ForEach(Console.WriteLine);
}
}
class FizzBuzz
{
string s = "";
public FizzBuzz(int n)
{
if ((n % 3) == 0) s += "Fizz";
if ((n % 5) == 0) s += "Buzz";
if (s.Equals("")) s += "hoge";
s = string.Format("{0,2}", n) + ":" + s;
}
public override string ToString()
{
return s;
}
}
|
LINQを使ってFizzBuzz
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | using System;
using System.Linq;
class Program
{
static void Main()
{
var FizzBuzz = from n in Enumerable.Range(1, 20)
select n % 15 == 0 ? new { Number = n, Message = "FizzBuzz" } :
n % 3 == 0 ? new { Number = n, Message = "Fizz" } :
n % 5 == 0 ? new { Number = n, Message = "Buzz" } : new { Number = n, Message = "hoge" };
foreach (var item in FizzBuzz)
{
Console.WriteLine("{0, 2} : {1}", item.Number, item.Message);
}
}
}
|





raynstard
#3758()
Rating0/2=0.00
[ reply ]