-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22_names_scores.cpp
69 lines (60 loc) · 1.26 KB
/
22_names_scores.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
67
68
69
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <vector>
using namespace std;
typedef vector<string> vs;
typedef vector<int> vi;
double score(string s);
void sort(vs::iterator begin, vs::iterator end);
int main()
{
fstream f;
vs names;
string s;
double sc = 0;
f.open("names.txt",ios::in);
while(getline(f,s,','))
names.push_back(s);
sort(names.begin(), names.end());
for(vs::iterator i = names.begin(); i!= names.end(); i++)
// if((i - names.begin() + 1 == 938))
// cout<<(i-names.begin() + 1)<<" "<<*i<<" "<<score(*i)<<endl;
sc += score(*i) * (double)(1 + i - names.begin());
cout<<setprecision(10)<<sc;
}
double score(string s)
{
double sum = 0;
for(string::iterator i = s.begin(); i!= s.end(); i++)
sum += (int)(*i - 'A' + 1);
return sum;
}
void sort(vs::iterator begin, vs::iterator end)
{
if(end-begin == 1)
return;
vs::iterator middle = begin + (end-begin)/2, i,j;
string temp;
temp = *middle;
*middle = *(end-1);
*(end-1) = temp;
j = begin;
for(i=begin;i!=end-1;i++)
{
if(*i < *(end-1))
{
temp = *i;
*i = *j;
*j = temp;
j++;
}
}
temp = *(end-1);
*(end-1) = *j;
*j = temp;
sort(begin,j);
sort(j,end);
return;
}