-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathRomanPalindromes.java
65 lines (56 loc) · 1.83 KB
/
RomanPalindromes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// uses a palindrome of some input roman numeral to get the number
// problem #8 of the ACM 2009 Regional Competition
public class RomanPalindromes{
// variables to test string
private static final String INPUT = "MCMLXXIV";
private static int sum = 0;
// main test of the input string
public static void main(String[] args){
// array that holds values of Roman Numerals
String[][] array = new String[7][2];
array[0][0] = "I";
array[0][1] = "1";
array[1][0] = "V";
array[1][1] = "5";
array[2][0] = "X";
array[2][1] = "10";
array[3][0] = "L";
array[3][1] = "50";
array[4][0] = "C";
array[4][1] = "100";
array[5][0] = "D";
array[5][1] = "500";
array[6][0] = "M";
array[6][1] = "1000";
// creates the roman numeral palindrome
int count = 0;
String palindrome = "";
// if length is odd
if(INPUT.length() % 2 == 1)
count++;
for(int i = 1; i <= (INPUT.length() -count); i++)
palindrome = palindrome + INPUT.charAt(INPUT.length() - i);
palindrome = palindrome + INPUT;
// calculates the roman numeral palindrome number
for(int j = 0; j < (palindrome.length() - 1); j++){
int pairSum = 0;
int first = 0;
int second = 0;
for(int k = 0; k < 7; k++){
if(palindrome.substring(j, j + 1).equals(array[k][0]))
first = Integer.parseInt(array[k][1]);
if(palindrome.substring(j + 1, j + 2).equals(array[k][0]))
second = Integer.parseInt(array[k][1]);
}
if(first < second)
pairSum = second - first;
else
pairSum = second + first;
sum = sum + pairSum;
}
// output of results
System.out.println("Input: " + INPUT);
System.out.println("Palindrome: " + palindrome);
System.out.println("Palindrome Number: " + sum);
}
}