-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQ22.cpp
66 lines (62 loc) · 1.53 KB
/
Q22.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 <iostream>
#include <fstream>
#include <string>
#define LENGTH 5163
bool is_pair_sorted(std::string first, std::string second) {
int a = first.length();
int b = second.length();
int c = a > b ? a : b;
for (int i = 0; i < a; i++) {
if (first[i] < second[i]) {
return true;
} else if (first[i] > second[i]) {
return false;
}
}
return a > b ? false : true;
}
void bubble_sort_string(std::string array[], int length) {
for (int i = 0; i < length -1; i++) {
bool flag = true;
for(int j = 0; j < length-i-1; j++) {
if (is_pair_sorted(array[j], array[j+1]) == false) {
std::string temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
flag = false;
}
}
if (flag) break;
}
}
int get_char_point(std::string name) {
int sum = 0;
for (int i = 0; i < name.length(); i++) {
sum += ((int)name[i])-64;
}
return sum;
}
int main() {
std::ifstream input("Q22.txt", std::ios::in);
std::string names[LENGTH];
char c;
int l=0;
while(!input.eof()) {
input.get(c);
switch (c) {
case '"':
break;
case ',':
l++;
break;
default:
names[l] += c;
}
}
bubble_sort_string(names, LENGTH);
int sum = 0;
for (int i = 0; i < LENGTH; i++) {
sum += (i+1) * get_char_point(names[i]);
}
std::cout << "Total score: " << sum << '\n';
}