Project Euler Homework

022.cpp

Go to the documentation of this file.
00001 
00009 #include <iostream>
00010 #include <fstream>
00011 #include <list>
00012 using namespace std;
00013 /*
00014 const int length = 9;
00015 char *name_list[] =
00016   { "MARY", "PATRICIA", "LINDA", "BARBARA", "ELIZABETH", "JENNIFER", "MARIA",
00017   "SUSAN", "MARGARET"
00018 };*/
00019 
00020 /*
00021 * Convert a word to checksum
00022 * @return checksum of 64bit unsigned integer
00023 */
00024 unsigned long long
00025 name2id (char *name)
00026 {
00027   unsigned long long id = 0;
00028   unsigned k = 0;
00029   while (*name != 0 && k < 13)
00030     {
00031       id = id * 27 + (*name - 'A' + 1);
00032       name++;
00033       k++;
00034     }
00035   for (; k < 13; k++)
00036     id *= 27;                   //0 = empty space
00037   return id;
00038 }
00039 
00040 /*
00041 * Find score of the word using scheme defined in problem 22
00042 */
00043 unsigned
00044 id2score (unsigned long long id)
00045 {
00046   unsigned sum = 0;
00047   while (id > 0)
00048     {
00049       unsigned tmp = id % 27;
00050       if (tmp != 0)             //skip trailing space
00051         sum += tmp;
00052       id /= 27;
00053     }
00054   return sum;
00055 }
00056 
00057 /*
00058 * Convert checksum to original word
00059 */
00060 void
00061 id2name (unsigned long long id)
00062 {
00063   list < char >name;
00064   while (id > 0)
00065     {
00066       unsigned tmp = id % 27;
00067       if (tmp != 0)             //skip trailing space
00068         name.push_front (tmp - 1 + 'A');
00069       id /= 27;
00070     }
00071   list < char >::iterator p;
00072   for (p = name.begin (); p != name.end (); p++)
00073     cout << *p;
00074 }
00075 
00076 /*
00077 * Calculate total score of a list
00078 */
00079 unsigned long long
00080 total_score (list < unsigned long long >&intlist)
00081 {
00082   unsigned long long sum = 0;
00083 
00084   list < unsigned long long >::iterator p = intlist.begin ();
00085   for (unsigned index = 1; p != intlist.end (); index++, p++)
00086     sum += index * id2score (*p);
00087   return sum;
00088 }
00089 
00090 /*
00091 * Print the words and score in a table
00092 */
00093 void
00094 print (list < unsigned long long >&intlist, short type = 0)
00095 {
00096   list < unsigned long long >::iterator p;
00097 
00098   switch (type)
00099     {
00100     case 0:                     //0 = print id only
00101       for (p = intlist.begin (); p != intlist.end (); p++)
00102         cout << *p << ' ';
00103       break;
00104     case 1:                     //1 = print name only
00105       for (p = intlist.begin (); p != intlist.end (); p++)
00106         {
00107           id2name (*p);
00108           cout << ' ';
00109         }
00110       break;
00111     case 2:                     //2 = print score only
00112       for (p = intlist.begin (); p != intlist.end (); p++)
00113         cout << id2score (*p) << ' ';
00114       break;
00115     }
00116   cout << endl;
00117 }
00118 
00119 /*
00120 * Import words from text file
00121 */
00122 void
00123 import (list < unsigned long long >*intlist)
00124 {
00125   ifstream input;
00126   input.open ("022.txt");
00127 
00128   char name[13];
00129   do
00130     {
00131       input >> name;
00132       intlist->push_front (name2id (name));
00133     }
00134   while (!input.eof ());
00135   intlist->pop_front(); //the last one is dublicated?
00136 }
00137 
00138 int
00139 main ()
00140 {
00141 
00142   list < unsigned long long >id_list;
00143   import (&id_list);
00144   //print (id_list, 1);
00145 
00146   id_list.sort ();
00147   //print (id_list, 1);print(id_list,2);
00148 
00149   cout << total_score (id_list) << endl;
00150   return 0;
00151 }
00152 
 All Classes Files Functions Variables