I know I didn't post anything here about this before, but I participated in the regional qualifiers for a programming competition over the weekend.
Out of 8 problems, there were 2 teams out of over 100 that got all 8... My team got 1.
For some reason my Roman Numeral summer doesn't work... And that cost us quite a bit...
I've tested using their examples, and a few of my own, and couldn't find anything that broke it until tonight...
The following is the input that breaks the program:
4
IX XC CM IV
For some reason it returns MCIII, which is a full 100 higher than it should be...
Input is:
no.of.numerals
numerals seperated by whitespace
And the entire frickin' code:
import java.util.Scanner;
public class A
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int qty = sc.nextInt();
int caseno = 1;
while(qty != 0)
{
sc.nextLine();
long sum = 0;
for(int i = 0; i < qty; i++)
{
String roman = sc.next();
sum += romanToLong(roman);
}
System.out.println("Case " + longToRoman(caseno) + ": " + longToRoman(sum));
caseno++;
qty = sc.nextInt();
}
}
public static long romanToLong(String s)
{
long temp = 0;
for(int i = 0; i < s.length(); i++)
{
switch(s.charAt(i))
{
case('M'):
temp += 1000;
break;
case('D'):
temp += 500;
break;
case('C'):
if(i < s.length() - 1 && s.charAt(i+1) == 'M')
{
temp += 900;
i++;
}
if(i < s.length() - 1 && s.charAt(i+1) == 'D')
{
temp += 400;
i++;
}
else
{
temp += 100;
}
break;
case('L'):
temp += 50;
break;
case('X'):
if(i < s.length() - 1 && s.charAt(i+1) == 'L')
{
temp += 40;
i++;
}
else if(i < s.length() - 1 && s.charAt(i+1) == 'C')
{
temp += 90;
i++;
}
else if(i < s.length() - 1 && s.charAt(i+1) == 'D')
{
temp += 490;
i++;
}
else if(i < s.length() - 1 && s.charAt(i+1) == 'M')
{
temp += 990;
i++;
}
else temp += 10;
break;
case('V'):
temp += 5;
break;
case('I'):
if(i < s.length() - 1 && s.charAt(i+1) == 'V')
{
temp += 4;
i++;
}
else if(i < s.length() - 1 && s.charAt(i+1) == 'X')
{
temp += 9;
i++;
}
else if(i < s.length() - 1 && s.charAt(i+1) == 'L')
{
temp += 49;
i++;
}
else if(i < s.length() - 1 && s.charAt(i+1) == 'C')
{
temp += 99;
i++;
}
else if(i < s.length() - 1 && s.charAt(i+1) == 'D')
{
temp += 499;
i++;
}
else if(i < s.length() - 1 && s.charAt(i+1) == 'M')
{
temp += 999;
i++;
}
else temp+= 1;
break;
}
}
return temp;
}
public static String longToRoman(long n)
{
String temp = "";
if(n >= 1000)
{
for(int i = 0; i < (int)(n/1000); i++)
{
temp += "M";
}
n = n % 1000;
}
if(n >= 900)
{
temp += "CM";
n = n % 900;
}
if(n >= 500)
{
temp += "D";
n = n % 500;
}
if(n >= 400)
{
temp += "CD";
n = n % 400;
}
if(n > 100)
{
for(int i = 0; i < (int)(n / 100); i++)
{
temp += "C";
}
n = n % 100;
}
if(n >= 90)
{
temp += "XC";
n = n % 90;
}
if(n >= 50)
{
temp += "L";
n = n % 50;
}
if(n >= 40)
{
temp += "XL";
n = n % 40;
}
if(n >= 10)
{
for(int i = 0; i < (int)(n / 10); i++)
{
temp += "X";
}
n = n % 10;
}
if(n >= 9)
{
temp += "IX";
n = n % 9;
}
if(n >= 5)
{
temp += "V";
n = n % 5;
}
if(n >= 4)
{
temp += "IV";
n = n % 4;
}
for(int i = 0; i < n; i++)
{
temp += "I";
}
return temp;
}
}