new semester here we go
This commit is contained in:
127
338/roman/Roman.java
Normal file
127
338/roman/Roman.java
Normal file
@@ -0,0 +1,127 @@
|
||||
import java.util.Scanner;
|
||||
/**
|
||||
* Roman numerals
|
||||
* I 1
|
||||
* V 5
|
||||
* X 10
|
||||
* L 50
|
||||
* C 100
|
||||
* D 500
|
||||
* M 1000
|
||||
*
|
||||
* Numbers are written in decreasing value
|
||||
* Example: XII is 12, not IIX or IXI
|
||||
*
|
||||
* But there are special rules:
|
||||
* I can come before V or X IV = 4, IX = 9
|
||||
* X can come before L or C XL = 40, XC = 90
|
||||
* C can come before D or M CD = 400 CM = 900
|
||||
*/
|
||||
public class Roman {
|
||||
|
||||
public static String intToRoman(int n) {
|
||||
String ret = "";
|
||||
int val = n;
|
||||
// dealing with the thousands case
|
||||
if(n > 1000) {
|
||||
int m = val / 1000;
|
||||
for(int i =0;i<m;i++) {
|
||||
ret += "M";
|
||||
val -= 1000;
|
||||
}
|
||||
}
|
||||
// 900 case
|
||||
if(val>=900) {
|
||||
ret += "CM";
|
||||
val -= 900;
|
||||
}
|
||||
// 600-800 range
|
||||
if(val>=600) {
|
||||
int c = (val - 500)/100;
|
||||
for(int i =0;i<c;i++) {
|
||||
ret += "C";
|
||||
}
|
||||
val -= 500;
|
||||
}
|
||||
// special case 500
|
||||
if(val>=500) {
|
||||
ret += "D";
|
||||
val -= 500;
|
||||
}
|
||||
// 400 special case
|
||||
if(val>=400) {
|
||||
ret +="CD";
|
||||
val -= 400;
|
||||
}
|
||||
// 100 - 300 range
|
||||
if(val>=100) {
|
||||
int x = (val - 100)/100;
|
||||
for(int i =0;i<x;i++) {
|
||||
ret += "X";
|
||||
}
|
||||
val -= 100;
|
||||
}
|
||||
// sub 100 range now
|
||||
// 90 special case first
|
||||
if(val>=90) {
|
||||
val -=90;
|
||||
ret += "XC";
|
||||
}
|
||||
if(val>=50) {
|
||||
ret += "L";
|
||||
int L=(val-50)/10;
|
||||
|
||||
for(int i =0;i<L;i++) {
|
||||
ret += "X";
|
||||
}
|
||||
val -= 50;
|
||||
}
|
||||
if(val>=40) {
|
||||
ret += "XL";
|
||||
val -= 40;
|
||||
}
|
||||
if(val>=10) {
|
||||
int x = (val - 10)/10;
|
||||
for(int i =0;i<x;i++) {
|
||||
ret += "X";
|
||||
}
|
||||
val -= 10;
|
||||
}
|
||||
if(val==9) {
|
||||
ret += "IX";
|
||||
return ret;
|
||||
}
|
||||
if(val>5){
|
||||
int c = (val - 5);
|
||||
for(int i =0;i<c;i++) {
|
||||
ret += "I";
|
||||
}
|
||||
val -=5;
|
||||
}
|
||||
if(val==5) {
|
||||
ret += "V";
|
||||
return ret;
|
||||
}
|
||||
if(val==4) {
|
||||
ret += "IV";
|
||||
return ret;
|
||||
}
|
||||
return ret;
|
||||
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Scanner in = new Scanner(System.in);
|
||||
while (true) {
|
||||
System.out.print("Enter Roman Number or q for quit. ");
|
||||
String x = in.nextLine();
|
||||
if (x.equals("q"))
|
||||
return;
|
||||
|
||||
// now we deal with normal cases
|
||||
int n = Integer.parseInt(x);
|
||||
System.out.println(n + " : " + intToRoman(n));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
21
338/roman/makefile
Normal file
21
338/roman/makefile
Normal file
@@ -0,0 +1,21 @@
|
||||
cc=~/Downloads/jdk-11.0.2/bin/javac
|
||||
fxlib=--module-path /home/shockrah/Downloads/javafx-sdk-11.0.2/lib
|
||||
ctrl=--add-modules javafx.controls
|
||||
|
||||
env=~/Downloads/jdk-11.0.2/bin/java
|
||||
jfile="Roman.java"
|
||||
cfile="Roman"
|
||||
|
||||
default:
|
||||
# takes a java file as entry to build
|
||||
#$(cc) $(fxlib) $< $(ctr)
|
||||
$(cc) $(jfile)
|
||||
|
||||
|
||||
# ouchie
|
||||
run:
|
||||
#$(env) $(fxlib) $(ctrl) $(cfile)
|
||||
$(env) $(cfile)
|
||||
|
||||
clean:
|
||||
rm -f *class
|
||||
Reference in New Issue
Block a user