Return to Snippet

Revision: 66263
at April 9, 2014 09:08 by omarelsaid


Initial Code
#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");
}

Initial URL

                                

Initial Description
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.

Initial Title
Sum of name scores

Initial Tags

                                

Initial Language
C++