#16596: 不明白為甚麼會錯?


a.meowalien@gmail.com (喵星人)

學校 : 不指定學校
編號 : 87149
來源 : []
最後登入時間 :
2018-10-20 08:22:50
a013. 羅馬數字 -- NPSC 模擬試題 | From: [220.130.136.72] | 發表日期 : 2019-01-20 11:57

我已經檢查並測試過很多次了,實在找不出問題在哪裡。
所以我稍微改了一下我的程式,讓他自動產生測資跑個二十萬次看看,依然看不出問題在哪裡,實在沒辦法了,只好發問了。

錯誤訊息是這樣:
您的程式被監控系統中斷,可能是程式無法正常結束所導致。 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 14 at A013.toRomanNumerals(A013.java:103) (改完是124行) at A013.main(A013.java:32) (改完是38行)


我的程式是這樣的:

package main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

public class A013 {
/**
* 由小到大排列的羅馬數字陣列
* */
private static final char[] RomanNumer = new char[] { 'I', 'V', 'X', 'L', 'C', 'D', 'M' };
/**
* 由小到大排列的阿拉伯數字陣列
* */
private static final int[] ArabicNumer = new int[] { 1, 5, 10, 50, 100, 500, 1000 };



public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String in;
int count = 0;

// while (!(in = br.readLine()).equals("#")) {
in = getTestString();
while( count != 200000 ) {
count++;
String[] inArray = in.split(" ");
int a = Integer.valueOf(inArray[0]);
int b = Integer.valueOf(inArray[1]);
System.out.println(inArray[0] + " - " + inArray[1] + " = " + (a-b));
inArray[0] = toRomanNumerals(a);
inArray[1] = toRomanNumerals(b);
System.out.println(inArray[0] + " - " + inArray[1]);
// System.out.println(toArabicNumerals(inArray[0]) + " - " + toArabicNumerals(inArray[1]));

int ans = toArabicNumerals(inArray[0]) - toArabicNumerals(inArray[1]);
System.out.println(toRomanNumerals(ans));

System.out.println(ans);
if((a-b) != ans) {
throw new RuntimeException();
}
in = getTestString();
System.out.println("----------");
}
}

/**
* 隨機取得測試資料
* */
private static String getTestString() {
int a = new Random().nextInt(3999)+1;
int b = a==1 ? a: new Random().nextInt(a-1)+1;

return String.format("%d %d", a,b);
}
/**
* 查詢指定羅馬數字的索引位置
* */
private static int getNumerIndex(char romanNumer) {
for (int i = 0; i < RomanNumer.length; i++) {
if (RomanNumer[i] == romanNumer) {
return i;
}
}
throw new RuntimeException();
}
/**
* 羅馬轉阿拉伯
* */
private static int toArabicNumerals(String inArray) {
int total = 0;
char[] inCArray = inArray.toCharArray();
int[] inIArray = new int[inCArray.length];
inIArray[0] = ArabicNumer[getNumerIndex(inCArray[0])];

for (int i = 1; i < inCArray.length; i++) {
if (((inCArray[i - 1] == 'I' || inCArray[i - 1] == 'X' || inCArray[i - 1] == 'C')
&& (getNumerIndex(inCArray[i - 1]) == (getNumerIndex(inCArray[i]) - 1)))
|| (inCArray[i] == 'M' && inCArray[i - 1] == 'C') || (inCArray[i] == 'C' && inCArray[i - 1] == 'X')
|| (inCArray[i] == 'X' && inCArray[i - 1] == 'I')) {
inIArray[i - 1] = -inIArray[i - 1];
inIArray[i] = ArabicNumer[getNumerIndex(inCArray[i])];
} else {

inIArray[i] = ArabicNumer[getNumerIndex(inCArray[i])];
}
}
for (int x : inIArray) {
total += x;
}
return total;
}

/**
* 阿拉伯轉羅馬
* */
private static String toRomanNumerals(int input) {
if (input == 0)
return "ZERO";
int digits = getNumLenght(input);
int cinPutN = -1;
char[] cinPut = new char[digits * 7];
int digitMum = getDigitMum(input, digits);
while (digits != 0) {
if (digits != 4) {
if (digitMum == 9) {
cinPut[++cinPutN] = RomanNumer[getOdd(digits) - 1];
cinPut[++cinPutN] = RomanNumer[getOdd(digits + 1) - 1];
} else if (digitMum >= 5) {
cinPut[++cinPutN] = RomanNumer[getEven(digits) - 1];
digitMum -= 5;
while (digitMum != 0) {
cinPut[++cinPutN] = RomanNumer[getOdd(digits) - 1];
digitMum--;
}
} else if (digitMum == 4) {
cinPut[++cinPutN] = RomanNumer[getOdd(digits) - 1];
cinPut[++cinPutN] = RomanNumer[getEven(digits) - 1];
} else {
while (digitMum != 0) {
cinPut[++cinPutN] = RomanNumer[getOdd(digits) - 1];
digitMum--;
}
}
} else {
while (digitMum != 0) {
cinPut[++cinPutN] = 'M';
digitMum--;
}
}

digitMum = getDigitMum(input, --digits);
}

return new String(cinPut).trim();

}


/**
* 取得指定順位的偶數
* */
private static int getEven(int order) {
return 2 * order;
}

/**
* 取得指定順位的奇數
* */
private static int getOdd(int order) {
return 2 * order - 1;
}

/**
* 取得數字長度
* */
private static int getNumLenght(int num) {
num = num > 0 ? num : -num;
if (num == 0) {
return 1;
}
return (int) Math.log10(num) + 1;
}

/**
* 取得指定位數的數字
* */
private static int getDigitMum(int number, int position) {
return (int) (number / Math.pow(10, position - 1)) % 10;
}
}

 
ZeroJudge Forum