??? 古羅馬帝國開創了輝煌的人類文明,但他們的數字表示法的確有些繁瑣,尤其在表示大數的時候,現在看起來簡直不能忍受,所以在現代很少使用了。之所以這樣,不是因為發明表示法的人的智力的問題,而是因為一個宗教的原因,當時的宗教禁止在數字中出現0的概念!
??? 羅馬數字的表示主要依賴以下幾個基本符號:
??? I? 1
??? V? 5
??? X? 10
??? L? 50
??? C? 100
??? D? 500
??? M? 1000
??? 這里,我們只介紹一下1000以內的數字的表示法。
??? 單個符號重復多少次,就表示多少倍。最多重復3次。比如:CCC表示300? XX表示20,但150并不用LLL表示,這個規則僅適用于I X C M。
??? 如果相鄰級別的大單位在右,小單位在左,表示大單位中扣除小單位。比如:IX表示9? IV表示4? XL表示40
??? 49 = XLIX
??? 更多的示例參見下表,你找到規律了嗎?
?? ?
I,1
II,2
III,3
IV,4
V,5
VI,6
VII,7
VIII,8
IX,9
X,10
XI,11
XII,12
XIII,13
XIV,14
XV,15
XVI,16
XVII,17
XVIII,18
XIX,19
XX,20
XXI,21
XXII,22
XXIX,29
XXX,30
XXXIV,34
XXXV,35
XXXIX,39
XL,40
L,50
LI,51
LV,55
LX,60
LXV,65
LXXX,80
XC,90
XCIII,93
XCV,95
XCVIII,98
XCIX,99
C,100
CC,200
CCC,300
CD,400
D,500
DC,600
DCC,700
DCCC,800
CM,900
CMXCIX,999
??? 本題目的要求是:請編寫程序,由用戶輸入若干個羅馬數字串,程序輸出對應的十進制表示。
??? 輸入格式是:第一行是整數n,表示接下來有n個羅馬數字(n<100)。以后每行一個羅馬數字。羅馬數字大小不超過999。
??? 要求程序輸出n行,就是羅馬數字對應的十進制數據。
??? 例如,用戶輸入:
3
LXXX
XCIII
DCCII
??? 則程序應該輸出:
80
93
702
/* 方法一 */ import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n = sc.nextInt();String[] a = new String[n];for (int i = 0; i < n; i++)a[i] = sc.next();sc.close();for (int i = 0; i < n; i++)System.out.println(check(a[i]));}static int check(String s) {char[] chars = s.toCharArray();int N = 0;for (int i = 0; i < chars.length;) {int t = 0;if (i == chars.length - 1) {t = f(chars[i]);i++;}if (i + 1 < chars.length)if (isIXCM(chars[i])) {int count = 1;int j = i+1;while (j < chars.length && chars[i] == chars[j] && count <= 3) {count++;j++;}if (count > 1) {t = f(chars[i]) * count;i += count;} else if (count == 1) {if (f(chars[i]) < f(chars[i + 1])) {t = f(chars[i + 1]) - f(chars[i]);i += 2;} else if (f(chars[i]) >= f(chars[i + 1])) {t = f(chars[i]);i++;}}} else {if (f(chars[i]) < f(chars[i + 1])) {t = f(chars[i + 1]) - f(chars[i]);i += 2;} else if (f(chars[i]) >= f(chars[i + 1])) {t = f(chars[i]);i++;}}N += t;}return N;}static boolean isIXCM(char c) {return c == 'I' || c == 'X' || c == 'C' || c == 'M';}static int f(char c) {int n = 0;switch (c) {case 'I':n = 1;break;case 'V':n = 5;break;case 'X':n = 10;break;case 'L':n = 50;break;case 'C':n = 100;break;case 'D':n = 500;break;case 'M':n = 1000;break;}return n;}}
/* 方法二:建立一個表,每兩個字符匹配 */import java.util.Scanner;public class Test2 {static int[][] table;static int M = 7, N = 7;public static void main(String[] args) {createTable();Scanner sc = new Scanner(System.in);int n = sc.nextInt();String[] a = new String[n];for (int i = 0; i < n; i++)a[i] = sc.next();sc.close();createTable();for (int i = 0; i < n; i++)System.out.println(check(a[i]));}static int check(String s) {char[] chars = s.toCharArray();int T = 0;int i = 0;while (i + 1 < chars.length) {T += table[charToIndex(chars[i])][charToIndex(chars[i + 1])];i += 2;}if (i == chars.length - 1)T += charToInt(chars[i]);return T;}static void createTable() {table = new int[M][N];for (int i = 0; i < M; i++) {for (int j = 0; j < N; j++) {table[i][j] = f3(i, j);}}}static int f3(int i, int j) {int t1 = charToInt(indexToChar(i)), t2 = charToInt(indexToChar(j));if (t2 > t1) {return t2 - t1;}return t1 + t2;}static int charToInt(char c) {int n = 0;switch (c) {case 'I': n = 1;break;case 'V': n = 5;break;case 'X': n = 10;break;case 'L': n = 50;break;case 'C': n = 100;break;case 'D': n = 500;break;case 'M': n = 1000;break;}return n;}static char indexToChar(int i){switch (i){case 0:return 'I';case 1:return 'V';case 2:return 'X';case 3:return 'L';case 4:return 'C';case 5:return 'D';}return 'M';}static int charToIndex(char c){switch (c){case 'I':return 0;case 'V':return 1;case 'X':return 2;case 'L':return 3;case 'C':return 4;case 'D':return 5;}return 6;} }
?