Microsoft Interview Question
Software Engineer in Testspublic class Solution {
private static int[] bases = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9,
5, 4, 1 };
private static HashMap<Integer, String> map = new HashMap<Integer, String>();
private static void setup() {
map.put(1, "I");
map.put(4, "IV");
map.put(5, "V");
map.put(9, "IX");
map.put(10, "X");
map.put(40, "XL");
map.put(50, "L");
map.put(90, "XC");
map.put(100, "C");
map.put(400, "CD");
map.put(500, "D");
map.put(900, "CM");
map.put(1000, "M");
}
public String intToRoman(int num) {
setup();
String result = new String();
for (int i : bases) {
while (num >= i) {
result += map.get(i);
num -= i;
}
}
return result;
}
}
this puts people who are unfamiliar with roman numbers at a disadvantage (e.g. native indians)
- Anonymous October 08, 2009