Dalek
BAN USER
Comments (4)
Reputation 35
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
class RomanNumeral {
public static int getNumberr (char c) {
switch(c) {
case 'I':
return 1;
case 'V':
return 5;
case 'X':
return 10;
case 'L':
return 50;
case 'C':
return 100;
case 'D':
return 500;
case 'M':
return 1000;
}
return 0;
}
public static int getNumber(String roman) {
int x = 0;
for (int i = 0; i < roman.length(); i++) {
int a = getNumberr(roman.charAt(i));
int b = (i+1) < roman.length() ? getNumberr(roman.charAt(i+1)) : 0;
if (a < b) {
x = x+ (b - a);
i++;
}
else {
x = x+ a;
}
}
return x;
}
public static void main(String args[]) {
String str = "MMMMCMXCIX";
System.out.println(" value is "+getNumber(str));
}
}
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
The idea is simple.
- Dalek October 22, 2015For all the points on the plane, just grab their x value. Then search in the consumer closest value to x. Then that is the closest point. The key here is consumer is a simple straight line. So the hypotenuse cannot be made smaller by picking far x values.