tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
41
Task/Roman-numerals-Encode/Java/roman-numerals-encode.java
Normal file
41
Task/Roman-numerals-Encode/Java/roman-numerals-encode.java
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
public class RN {
|
||||
|
||||
enum Numeral {
|
||||
I(1), IV(4), V(5), IX(9), X(10), XL(40), L(50), XC(90), C(100), CD(400), D(500), CM(900), M(1000);
|
||||
int weight;
|
||||
|
||||
Numeral(int weight) {
|
||||
this.weight = weight;
|
||||
}
|
||||
};
|
||||
|
||||
public static String roman(long n) {
|
||||
|
||||
if( n <= 0) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
StringBuilder buf = new StringBuilder();
|
||||
|
||||
final Numeral[] values = Numeral.values();
|
||||
for (int i = values.length - 1; i >= 0; i--) {
|
||||
while (n >= values[i].weight) {
|
||||
buf.append(values[i]);
|
||||
n -= values[i].weight;
|
||||
}
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public static void test(long n) {
|
||||
System.out.println(n + " = " + roman(n));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
test(1999);
|
||||
test(25);
|
||||
test(944);
|
||||
test(0);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue