题目描述
大意就是我成了地球首富,然后有很多钱,但是地球已经不适合人类居住了,需要跑到银河系去做生意,但是银河系使用的是罗马字母表示钱,所以座位程序员的我打算自己写一个罗马字母和十进制数字互转的小程序,以帮助我做生意。
罗马字母有: I,V,X,L,C,D,M
转换规则:
1. I:可以表示十进制数字1,V:可以表示十进制数字5,X:可以表示十进制数字10,L:可以表示十进制数字50,C:可以表示十进制数字100,D:可以表示十进制数字500,M:可以表示十进制数字1000;
2.I, X, C, and M可以重复出现最多3次;
3.一般是从后往前排列:即MDCLXVI的顺序,当然也允许相邻的两个倒序,但是需要符合以下规则:
I :只能组合IV ,IX
X:只能组合XL,XC
C:只能组合CD,CM
V,L,D不能倒序
Test input:
glob is I
prok is V
pish is X
tegj is L
glob glob Silver is 34 Credits
glob prok Gold is 57800 Credits
pish pish Iron is 3910 Credits
how much is pish tegj glob glob ?
how many Credits is glob prok Silver ?
how many Credits is glob prok Gold ?
how many Credits is glob prok Iron ?
how much wood could a woodchuck chuck if a woodchuck could chuck wood ?
Test Output:
pish tegj glob glob is 42
glob prok Silver is 68 Credits
glob prok Gold is 57800 Credits
glob prok Iron is 782 Credits
I have no idea what you are talking about
解题
public class Roman {
char sysbol;
int value;
//only I,X,C,M can repeated
boolean allowRepeat;
//only I,X C can subtracted
//such as: IV,IX; XL,XC;CD,CM
boolean allowSubstracted;
//if allow subtracted, how distence?
//I,V,X,L,C,D,M
//IV,IX:distence is 2;
int substractedDestence;
public char getSysbol() {
return sysbol;
}
public void setSysbol(char sysbol) {
this.sysbol = sysbol;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public boolean isAllowRepeat() {
return allowRepeat;
}
public void setAllowRepeat(boolean allowRepeat) {
this.allowRepeat = allowRepeat;
}
public boolean isAllowSubstracted() {
return allowSubstracted;
}
public void setAllowSubstracted(boolean allowSubstracted) {
this.allowSubstracted = allowSubstracted;
}
public int getSubstractedDestence() {
return substractedDestence;
}
public void setSubstractedDestence(int substractedDestence) {
this.substractedDestence = substractedDestence;
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class RomanNumber {
private char[] allowRomans=new char[] {
'I',
'V',
'X',
'L',
'C',
'D',
'M'
};
private int[] vs = new int[]{
1,
5,
10,
50,
100,
500,
1000
};
List<Roman> featuredRomans = new ArrayList<Roman>();
public List<Roman> getRomans(){
return featuredRomans;
}
public RomanNumber() {
for (int i = 0; i < allowRomans.length; i++) {
Roman roman = new Roman();
roman.setSysbol(allowRomans[i]);
roman.setValue(vs[i]);
roman.setAllowRepeat(allowRomans[i] == 'I' || allowRomans[i] == 'X' || allowRomans[i] ==
'C' || allowRomans[i] == 'M');
roman.setAllowSubstracted(allowRomans[i] == 'I' || allowRomans[i] == 'X' || allowRomans[
i] == 'C');
roman.setSubstractedDestence((allowRomans[i] == 'I' || allowRomans[i] == 'X' ||
allowRomans[i] == 'C') ? 2 : 0);
featuredRomans.add(roman);
}
}
public int calculate(String exp) throws Exception {
if (exp.equals("")) return 0;
char[] romans = exp.toCharArray();
int result = 0;
if (romans.length == 1) {
for (Roman c : featuredRomans) {
if(c.getSysbol() == romans[0])
return c.getValue();
}
}
for (int i = 0; i <= romans.length - 1; i = i + 2) {
Roman current=null;
for (Roman c : featuredRomans) {
if(c.getSysbol() == romans[i])
current=c;
}
if (i >= romans.length - 1) {
result += current.getValue();
break;
}
Roman next =null;
for (Roman c : featuredRomans) {
if(c.getSysbol() == romans[i+1])
next=c;
}
if (current == null) {
throw new Exception("unexpected roman char:"+romans[i]);
}
if (next == null) {
throw new Exception("unexpected roman char:"+romans[i + 1]);
}
if (current.getValue() > next.getValue()) {
result += current.getValue();
i--;
} else if (current.getValue() == next.getValue()) {
if (!current.isAllowRepeat()) {
throw new Exception("Not allow {0} repeate"+current.getSysbol());
}
int repeatedCount = 2;
for (int j = i + 2; j < romans.length; j++) {
if (romans[j] != current.getSysbol()) break;
repeatedCount++;
if (repeatedCount > 3) {
throw new Exception("Not allow {0} repeated more than 3 times"+current.getSysbol());
}
}
result += current.getValue();
i--;
} else {
result += next.getValue() - current.getValue();
}
}
return result;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 从键盘接收数据
// 判断是否还有输入
RomanNumber rn = new RomanNumber();
if (scan.hasNextLine()) {
String str2 = scan.nextLine();
try {
System.out.println("result为:" + rn.calculate(str2));
} catch (Exception e) {
e.printStackTrace();
}
}
scan.close();
}
}