Its friday~

windows phone 7 series

Project Euler problem 17~

  1:     class Program
  2:     {
  3:         static void Main(string[] args)
  4:         {
  5:             var sw = new Stopwatch();
  6: 
  7:             Console.WriteLine("How many letters would be used in number range from 1 to 1000?");
  8:             sw.Start();
  9:             var sum1 = Test2(1000);
 10:             sw.Stop();
 11:             Console.WriteLine(string.Format("plain arithmetic brute force way(tick:{0}): {1}", sw.ElapsedTicks, sum1));
 12: 
 13:             sw.Reset();
 14: 
 15:             Console.WriteLine("Press any key to exit");
 16:             Console.ReadKey();
 17:         }
 18: 
 19:         static int Test2(int max)
 20:         {
 21:             var sum = 0;
 22:             var num = 0;
 23:             for (int i = 1; i <= max; i++)
 24:             {
 25:                 num = i;
 26:                 if (num >= 1000)
 27:                 {
 28:                     sum += 11;
 29:                     continue;
 30:                 }
 31: 
 32:                 if (num >= 100)
 33:                 {
 34:                     sum += (map_zero_to_nineteen[num / 100] + 7);
 35:                     num = num % 100;
 36:                     if (num > 0)
 37:                         sum += 3;
 38:                 }
 39: 
 40:                 if (num < 20)
 41:                     sum += map_zero_to_nineteen[num];
 42:                 else
 43:                     sum += (map_twenty_to_ninety[num / 10] + map_zero_to_nineteen[num % 10]);
 44:             }
 45:             return sum;
 46:         }
 47: 
 48:         static readonly int[] map_zero_to_nineteen = new int[]
 49:         {
 50:             0, //place holder for 0
 51:             "one"       .Length,
 52:             "two"       .Length,  
 53:             "three"     .Length,
 54:             "four"      .Length, 
 55:             "five"      .Length, 
 56:             "six"       .Length, 
 57:             "seven"     .Length,
 58:             "eight"     .Length,
 59:             "nine"      .Length,
 60:             "ten"       .Length,     
 61:             "eleven"    .Length,   
 62:             "twelve"    .Length,   
 63:             "thirteen"  .Length, 
 64:             "fourteen"  .Length, 
 65:             "fifteen"   .Length,  
 66:             "sixteen"   .Length,  
 67:             "seventeen" .Length,
 68:             "eighteen"  .Length, 
 69:             "nineteen"  .Length 
 70:         };
 71: 
 72:         static readonly int[] map_twenty_to_ninety = new int[]
 73:         {
 74:             0, //place holder for 0
 75:             0, //place holder for 1(10)
 76:             "twenty"  .Length,     
 77:             "thirty"  .Length,   
 78:             "forty"   .Length,   
 79:             "fifty"   .Length, 
 80:             "sixty"   .Length, 
 81:             "seventy" .Length,  
 82:             "eighty"  .Length,  
 83:             "ninety"  .Length,
 84:         };
 85:     }

Advertisements
This entry was posted in Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s