-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistantWords.java
More file actions
35 lines (28 loc) · 876 Bytes
/
distantWords.java
File metadata and controls
35 lines (28 loc) · 876 Bytes
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
package FML;
import java.util.Scanner;
import java.lang.Math;
public class distantWords {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int target = Integer.parseInt(sc.nextLine());
int numWords = Integer.parseInt(sc.nextLine());
int sumDistance = 0;
for (int i = 0; i < numWords; i++) {
String currentWord = sc.nextLine();
int currentScore = wordScore(currentWord);
int currentDistance = Math.abs(currentScore - target);
sumDistance += currentDistance;
System.out.printf("%s %s", currentWord, currentDistance);
System.out.println();
}
float avgDistance = sumDistance / (float) numWords;
System.out.printf("%.2f", avgDistance);
}
static int wordScore(String word) {
int sum = 0;
for (int i = 0; i < word.length(); i++) {
sum += ((int) word.toLowerCase().charAt(i)) - 96;
}
return sum;
}
}