# 【ProjectEuler】ProjectEuler_022

57 篇文章 0 订阅
55 篇文章 0 订阅
// Problem 22
// 19 July 2002
//
// Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.
//
// 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.
//
// What is the total of all the name scores in the file?

#include <iostream>
#include <windows.h>
#include <set>
#include <string>
using namespace std;

//获取字符串中所有字符位置之和，传入字符串全是大写英文字母
int GetStringSum(const string str)
{
int sum = 0;
int length = str.length();

for(int i = 0; i < length; i++)
{
sum += str[i] - 'A' + 1;
}

return sum;
}

void F1()
{
cout << "void F1()" << endl;

LARGE_INTEGER timeStart, timeEnd, freq;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&timeStart);

set<string> namesSet;

//获取总共的姓名数目
int length = sizeof(names) / sizeof(string);

//使用set插入排序
for(int i = 0; i < length; i++)
{
namesSet.insert(names[i]);
}

int totalValue = 0;
int index = 1;

//计算位置和字符总值的成绩并汇总到totalValue
for(set<string>::iterator it = namesSet.begin(); it != namesSet.end(); it++)
{
totalValue += index++*GetStringSum(*it);
}

cout << "总值为：" << totalValue << endl;

QueryPerformanceCounter(&timeEnd);
}

//主函数
int main()
{
F1();
return 0;
}

/*
void F1()

Total Milliseconds is 140.397

By GodMoon
*/

• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 0
评论
06-16 9895
03-30 9775
12-31 4910
03-09 3806
04-18 640
02-11 1万+
01-02 1737
12-11 2万+

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。