Project Euler Homework

017.cpp

Go to the documentation of this file.
00001 
00009 #include <iostream>
00010 using namespace std;
00011 
00012 //x,one,two,three,four,five,six,seven,eight,nine
00013 unsigned short unit_digits[] = { 0, 3, 3, 5, 4, 4, 3, 5, 5, 4 };
00014 
00015 //x,ten,twenty,thirty,forty,fifty,sixty,seventy,eighty,ninety
00016 unsigned short tenth_digits[] = { 0, 3, 6, 6, 5, 5, 5, 7, 6, 6 };
00017 
00018 //ten,eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen,eighteen,nineteen
00019 unsigned short special[] = { 3, 6, 6, 8, 8, 7, 7, 9, 8, 8 };
00020 
00021 unsigned
00022 letters (unsigned k)
00023 {
00024   unsigned sum = 0;
00025   if (k >= 100)
00026     {
00027       sum += unit_digits[k / 100] + 7;  //one hundred ...
00028       k %= 100;
00029       if (k > 0)
00030         sum += 3;               //...and...
00031     }
00032   if (k >= 20)
00033     {
00034       sum += tenth_digits[k / 10] + unit_digits[k % 10];        //...fourty one
00035       return sum;
00036     }
00037   else if (k >= 10)             //special case
00038     {
00039       sum += special[k % 10];   //...eleven
00040       return sum;
00041     }
00042   else
00043     {
00044       sum += unit_digits[k];    //...one
00045       return sum;
00046     }
00047 }
00048 int
00049 main ()
00050 {
00051   unsigned sum_of_letters = 0;
00052   for (unsigned k = 1; k <= 999; k++)
00053     sum_of_letters += letters (k);
00054 
00055   sum_of_letters += 3 + 8;      //one thousand
00056   cout << sum_of_letters << endl;
00057   return 0;
00058 }
00059 
 All Classes Files Functions Variables