トップページに戻る    次のC#のサンプルへ    前のC#のサンプルへ

Problem40 正の整数を順に連結して得られる文字列

問題

正の整数を順に連結して得られる以下の10進の無理数を考える

0.123456789101112131415161718192021...
小数第12位は1である.

dnで小数第n位の数を表す. d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000 を求めよ.


ソース

using System;

class Program
{
    static void Main()
    {
        var sb = new System.Text.StringBuilder();
        for (int I = 1; I < int.MaxValue; I++) {
            sb.Append(I.ToString());
            if (sb.Length >= 1000000) break;
        }
        int V1 = int.Parse(sb[1 - 1].ToString());
        int V2 = int.Parse(sb[10 - 1].ToString());
        int V3 = int.Parse(sb[100 - 1].ToString());
        int V4 = int.Parse(sb[1000 - 1].ToString());
        int V5 = int.Parse(sb[10000 - 1].ToString());
        int V6 = int.Parse(sb[100000 - 1].ToString());
        int V7 = int.Parse(sb[1000000 - 1].ToString());
        Console.WriteLine("{0}*{1}*{2}*{3}*{4}*{5}*{6}={7}", V1, V2, V3, V4, V5, V6, V7
                          , V1 * V2 * V3 * V4 * V5 * V6 * V7);
    }
}


実行結果

1*1*5*3*7*2*1=210


解説

StringBuilderで順に連結していってます。