-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution17.java
More file actions
49 lines (48 loc) · 1.47 KB
/
Copy pathSolution17.java
File metadata and controls
49 lines (48 loc) · 1.47 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
public class Solution17{
static String ones[] = {"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
static String tens[] = {"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
static String hundred = " hundred";
static String thousand = "thousand";
static String toEnglish(int num){
String result = "";
if(num < 20){
result += ones[num - 1];
}
else if(num < 100){
result += tens[(num/10) - 2];
if(num % 10 > 0){
result += " " + toEnglish(num % 10);
}
}
else if(num < 1000){
result += toEnglish(num/100) + hundred;
if(num % 100 > 0){
result += " and " + toEnglish(num % 100);
}
}
else {
if(num == 1000){
result += "one " + thousand;
}
if(num > 1000){
result += toEnglish(num/1000) + " " + thousand;
if(num % 1000 > 0){
result += " " + toEnglish(num % 1000);
}
}
}
return result;
}
public static void main(String[] args) {
int sum = 0;
for(int i = 1; i <= 1000; i++){
String english = toEnglish(i);
for(Character ch : english.toCharArray()){
if(Character.isLowerCase(ch)){
sum++;
}
}
}
System.out.println(sum);
}
}