-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem_22.java
41 lines (38 loc) · 995 Bytes
/
Problem_22.java
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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class Problem_22 {
public static void main(String[] args) {
File file = new File("Problem_22_Data.txt");
String dataString = "";
try {
Scanner data = new Scanner(file);
while(data.hasNextLine()) {
dataString = data.next();
}
data.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String[] names = dataString.split(",");
for (int i = 0; i < names.length; i++) {
names[i] = names[i].substring(1, names[i].length()-1);
}
Arrays.sort(names);
int answer = 0;
for (int i = 0; i < names.length; i++) {
int nameScore = getAlphabeticValue(names[i]);
nameScore *= i+1;
answer+=nameScore;
}
System.out.println(answer);
}
public static int getAlphabeticValue(String alphabet) {
int value = 0;
for (int i = 0; i < alphabet.length(); i++) {
value += (alphabet.charAt(i)-'A'+1);
}
return value;
}
}