-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path022.cpp
67 lines (51 loc) · 1.13 KB
/
022.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
#include <vector>
#include <fstream>
#include <sstream>
#include "lib/big_num.h"
std::vector<std::string> names;
int partition(int low, int high)
{
std::string pivot = names[high];
int i = low;
for (int j = low; j < high; j++) {
if (names[j] <= pivot) {
std::swap(names[i], names[j]);
i++;
}
}
std::swap(names[i], names[high]);
return i;
}
void sort_names(int low, int high)
{
if (low < high) {
int i = partition(low, high);
sort_names(low, i - 1);
sort_names(i + 1, high);
}
}
int get_score(std::string name)
{
int score = 0;
for (char& c : name)
score += ((int) c) - 64;
return score;
}
int main(int argc, char** argv)
{
BigNum* bignum = new BigNum(0);
std::ifstream file("data/p022_names.txt");
std::string line, name;
while (std::getline(file, line)) {
std::istringstream iss(line);
while (std::getline(iss, name, ',')) {
names.push_back(name.substr(1, name.length() - 2));
}
}
sort_names(0, names.size() - 1);
for (int i = 0; i < names.size(); i++)
bignum->add((i + 1) * get_score(names[i]));
bignum->print();
return 0;
}