LL Golf Hole 3 - 13日の金曜日を数え上げる
Posted feedbacks - C#
C#は捻らずに素直に書いた方が短くなりますね。
1 | using System;class P{static void Main(){DateTime d=DateTime.Now;int c=0;for(;d<=new DateTime(2013,12,31);d=d.AddDays(1)){if(d.Day==13&(int)(d.DayOfWeek)==5){Console.WriteLine(d);c++;}}Console.Write(c);}}
|
まずはfor文で。誰かLINQ版を書こうよ。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | const int WEEK = 7;
DateTime today = DateTime.Now.Date;
DateTime from = today.AddDays((DayOfWeek.Friday - today.DayOfWeek + WEEK) % WEEK);
DateTime to = new DateTime(2013, 12, 31);
int count = 0;
for (DateTime d = from; d <= to; d = d.AddDays(WEEK))
{
if (d.Day == 13)
{
Console.WriteLine(d.ToShortDateString());
count++;
}
}
Console.WriteLine("{0} days", count);
|
実はLINQも試したのですが、長くなったので投稿しませんでした。 LINQで抽出する対象のDateTimeのコレクションを作る為に結局ループを回しちゃってるんですよね。
1 | using System;using System.Collections.Generic;using System.Linq;class C{static void Main(){List<DateTime>l=new List<DateTime>();for(DateTime d=DateTime.Now;d<=new DateTime(2013,12,31);d=d.AddDays(1)){l.Add(d);}var v=from f in l where(int)(f.DayOfWeek)==5&f.Day==13select f.Date;foreach(var d in v){Console.WriteLine(d);}Console.Write(v.Count<DateTime>());}}
|
LINQのみ。 もうちょっと書きようがあるかなぁ?
1 2 3 4 5 6 7 8 9 10 11 12 | DateTime today = DateTime.Now.Date;
var q = from year in Enumerable.Range(today.Year, 2014 - today.Year)
from month in Enumerable.Range(1, 12)
let d = new DateTime(year, month, 13)
where (d.DayOfWeek == DayOfWeek.Friday) && (d >= today)
select d;
foreach (DateTime d in q)
{
Console.WriteLine("{0:yyyy/MM/dd(ddd)}", d);
}
Console.WriteLine(q.Count());
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | using System;
using System.Linq;
class Program
{
static void Main()
{
var d =
from n in Enumerable.Range(0, (new DateTime(2013, 12, 31) - DateTime.Today).Days)
where DateTime.Today.AddDays(n).Day == 13
where DateTime.Today.AddDays(n).DayOfWeek == DayOfWeek.Friday
select DateTime.Today.AddDays(n);
foreach (var item in d)
{
Console.WriteLine(item.ToShortDateString());
}
Console.WriteLine(d.Count());
}
}
|
LINQ 使って書いてみた。クエリ式嫌いなのでメソッドチェインで。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | using System;
using System.Linq;
class Program
{
static void Main()
{
var a = Enumerable.Range(0, (new DateTime(2013, 12, 31) - DateTime.Today).Days)
.Select(n => DateTime.Today.AddDays(n))
.Where(d => d.Day == 13 && d.DayOfWeek == DayOfWeek.Friday);
foreach (var d in a) Console.WriteLine(d.ToShortDateString());
Console.WriteLine(a.Count());
}
}
|




takano32
#6985()
[
Ruby
]
Rating4/8=0.50
今日から2013年12月31日までの、13日の金曜日とその総数を表示してください。
余力のあるものはこのプログラムを短くしてみたり、短くしてみたり、短くしてください。
※LL Future実行委員の高野光弘です。この出題は LL Future公式の出題であり、優れたものについてはLL Golfのセッションでご紹介させていただくかもしれません。ご理解の上、ご投稿ください。また、LL Futureのチケットは現在も発売中です。よろしければ、メインイベントの方にもぜひご参加ください。
see: DateTime - Rubyリファレンスマニュアル
Rating4/8=0.50-0+
[ reply ]