-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem017.java
More file actions
94 lines (86 loc) · 2.28 KB
/
problem017.java
File metadata and controls
94 lines (86 loc) · 2.28 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package project_beuler;
public class problem017 {
// If the numbers 1 to 5 are written out in words: one, two, three, four, five,
// then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
//
// If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words,
// how many letters would be used?
//
//
// NOTE: Do not count spaces or hyphens.
// For example, 342 (three hundred and forty-two) contains 23 letters
// and 115 (one hundred and fifteen) contains 20 letters.
// The use of "and" when writing out numbers is in compliance with British usage.
public static void main(String[] args) {
// TODO Auto-generated method stub
long sum = 0;
int n = 1000;
for(int i = 1; i <= n; i++)
sum+= numLetters(i);
System.out.println(sum);
}
public static int numLetters(int n) {
String length = thousands(n) + hundreds(n);
if(n > 100 && n%100 > 0)
length += "and";
length+= tens(n) + ones(n);
System.out.println(length);
return length.length();
}
public static String thousands(int n) {
int digit = 1000;
String append = "thousand";
if(n < digit)
return "";
return ones( (n/digit) % digit)+append;
}
public static String hundreds(int n) {
int digit = 100;
String append = "hundred";
if(n < digit || n == 1000)
return "";
return ones( (n/digit) % digit)+append;
}
public static String tens(int n) {
switch( (n/10)%10 ) {
case 9: return "ninety";
case 8: return "eighty";
case 7: return "seventy";
case 6: return "sixty";
case 5: return "fifty";
case 4: return "forty";
case 3: return "thirty";
case 2: return "twenty";
case 1: return teens(n%100);
default: return "";
}
}
public static String teens(int n) {
switch(n) {
case 19: return "teen";
case 18: return "een";
case 17: return "teen";
case 16: return "teen";
case 15: return "een";
case 14: return "teen";
case 13: return "een";
case 12: return "lve";
case 11: return "ven";
default: return "ten";
}
}
public static String ones(int n) {
switch(n%10) {
case 9: return "nine";
case 8: return "eight";
case 7: return "seven";
case 6: return "six";
case 5: return "five";
case 4: return "four";
case 3: return "three";
case 2: return "two";
case 1: return "one";
default: return "";
}
}
}