-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathProblem_42.java
More file actions
49 lines (45 loc) · 1.18 KB
/
Problem_42.java
File metadata and controls
49 lines (45 loc) · 1.18 KB
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
import java.io.*;
class Problem_42 {
public static void main(String[] args){
String[] names = new String[]{};
int count = 0;
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(new File("PE042_words.txt")));
String text = null;
while ((text = reader.readLine()) != null) {
text = text.substring(1, text.length() - 1);
names = text.split("\",\"");
for(int i = 0; i < names.length; i++) {
int numericValue = 0;
for(int j = 0; j < names[i].length(); j++) {
numericValue += (names[i].charAt(j) - 'A') + 1;
}
if(isTriangle(numericValue)) {
count++;
}
}
System.out.println(count);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
}
}
}
public static boolean isTriangle(int n) {
for(int i = 0; 0.5 * i * (i + 1) <= n; i++) {
if(0.5 * i * (i + 1) == n) {
return true;
}
}
return false;
}
}