/ Published in: C++
Reads in a comma separated text file of names and computes the total name score of all the names in the file.
For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.
Doing this for all the names in the text file and summing the scores is the objective of this code.
For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.
Doing this for all the names in the text file and summing the scores is the objective of this code.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#define _SCL_SECURE_NO_WARNINGS #include <iostream> #include <fstream> #include <vector> #include <boost/algorithm/string.hpp> #include <algorithm> void main() { //read in a comma separated list of names, with quotation marks. e.g //"ANTHONY","JAMES","TOM",... etc std::ifstream ifs("./names.txt"); std::vector<std::string> names; std::string line; while (std::getline(ifs, line)) boost::split(names, line, boost::is_any_of(",")); std::stable_sort(names.begin(), names.end()); unsigned long long int scoreSum = 0; for (unsigned int i = 0; i < names.size(); ++i) { unsigned int sum = 0; for (unsigned int j = 1; j < names[i].size() - 1; ++j) sum += names[i][j] - 'A' + 1; scoreSum += sum * (i + 1); } std::cout << scoreSum << std::endl; system("pause"); }