HIDORI on The Web

マネージコードしか書きたくない

0 から 1000 までに含まれる ‘0’ を数える

without comments

ネタ元: gist: 415551 – from http://d.hatena.ne.jp/os0x/20081115/1226770265- GitHub

Sum() しない版。

足し算で「個数を求める」のではなく、「数える」という点を強調したつもり。

あと、ネストをするとラムダ式の引数の名前考えるのがめんどくさい (^^; ので、SelectMany() を使って、早い段階でフラットな IEnumerable に変換してしまっているのもミソ。

using System;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(
                Enumerable.Range(0, 1001).SelectMany(_ => _.ToString()).Count(_ => _ == '0'));
        }
    }
}

Written by Hiroaki SHIBUKI

5月 27th, 2010 at 6:16 pm

Posted in 技術情報

Tagged with ,

Leave a Reply