【ProjectEuler】ProjectEuler_017

using System;
using System.Collections.Generic;
using System.Text;

namespace projecteuler017
{
    class Program
    {
        /// <summary>
        /// 1~19含的字母数
        /// </summary>
        static int[] d1_19 = new int[] { 0, 3, 3, 5, 4, 4, 3, 5, 5, 4, 3, 6, 6, 8, 8, 7, 7, 9, 8, 8 };
        
        /// <summary>
        /// 20,30,40……90含的字母数
        /// </summary>
        static int[] d20_90 = new int[] { 0, 0, 6, 6, 5, 5, 5, 7, 6, 6 };

        static void Main(string[] args)
        {
            F1();
        }

        private static void F1()
        {
            Console.WriteLine(new System.Diagnostics.StackTrace().GetFrame(0).GetMethod());
            DateTime timeStart = DateTime.Now;

            int sum = 0;
            for (int i = 1; i <=1000; i++)
            {
                sum += getLetterCount(i);
            }
            Console.WriteLine(sum);

            Console.WriteLine("Total Milliseconds is " + DateTime.Now.Subtract(timeStart).TotalMilliseconds + "\n\n");
        }

        private static int getLetterCount(int n)
        {
            if (n < 20)
            {
                return d1_19[n];
            }
            if (n % 10 == 0 && n <= 90)
            {
                return d20_90[n / 10];
            }
            if (n <= 99)
            {
                return d20_90[n / 10] + d1_19[n % 10];
            }
            if (n == 1000)
            {
                return 11;  //one thousand
            }
            if (n % 100 == 0)
            {
                return d1_19[n / 100] + 7;
            }
            return getLetterCount(n / 100 * 100) + 3 + getLetterCount(n % 100);
        }
    }
}

/*
Void F1()
21124
Total Milliseconds is 11.5015

By GodMoon
*/

你可能感兴趣的:(String,Class)