HOA管的太多了# Living
l*m
1 楼
JAVA 不太熟,尝试着快速写了下这两道题,请各位大牛帮忙看看啊,有没有什么
问题。当然我这里没有考虑大数问题,这个呆会我再想想,或者谁教我也好。
public class RomansToInt {
public static HashMap map;
public static int RomanToInt(String input) {
if (input == null || input.length() == 0) {
System.out.println("The input is not valid");
return -1;
}
int len = input.length();
if (len == 1) {
return map.get(input.charAt(0));
}
int result = 0;
int i = 0;
while (i < len) {
if ((i + 1) < len
&& map.get(input.charAt(i + 1)) > map.get(input.charAt(i
))) {
result += (map.get(input.charAt(i + 1)) - map.get(input
.charAt(i)));
i = i + 2;
} else {
result += map.get(input.charAt(i));
i = i + 1;
}
}
return result;
}
public static void main(String[] args) {
map = new HashMap();
map.put('I', 1);
map.put('V', 5);
map.put('X', 10);
map.put('L', 50);
map.put('C', 100);
map.put('D', 500);
map.put('M', 1000);
String s = "";
System.out.println(s + " : " + RomanToInt(s));
}
}
~~~~~~~~~~~~
Int 转罗马
class MapNode{
public int key;
public String value;
MapNode(int k, String str){
key = k;
value = str;
}
}
public class IntToRoman {
public static ArrayList map;
private static String IntToRomans(int input) {
if (input == 0) {
return null;
}
String result = "";
for(MapNode node:map){
int current = node.key;
while (input >= current) {
result += node.value;
input = input - current;
}
}
return result;
}
private static void Init() {
map = new ArrayList();
map.add(new MapNode(1000,"M"));
map.add(new MapNode(900,"CM"));
map.add(new MapNode(500,"D"));
map.add(new MapNode(400,"CD"));
map.add(new MapNode(100,"C"));
map.add(new MapNode(90,"XC"));
map.add(new MapNode(50,"L"));
map.add(new MapNode(40,"XL"));
map.add(new MapNode(10,"X"));
map.add(new MapNode(9,"IX"));
map.add(new MapNode(5,"V"));
map.add(new MapNode(4,"IV"));
map.add(new MapNode(1,"I"));
}
public static void main(String[] args) {
Init();
int testInput = 10;
System.out.println(testInput + " : " + IntToRomans(testInput));
}
}
问题。当然我这里没有考虑大数问题,这个呆会我再想想,或者谁教我也好。
public class RomansToInt {
public static HashMap
public static int RomanToInt(String input) {
if (input == null || input.length() == 0) {
System.out.println("The input is not valid");
return -1;
}
int len = input.length();
if (len == 1) {
return map.get(input.charAt(0));
}
int result = 0;
int i = 0;
while (i < len) {
if ((i + 1) < len
&& map.get(input.charAt(i + 1)) > map.get(input.charAt(i
))) {
result += (map.get(input.charAt(i + 1)) - map.get(input
.charAt(i)));
i = i + 2;
} else {
result += map.get(input.charAt(i));
i = i + 1;
}
}
return result;
}
public static void main(String[] args) {
map = new HashMap
map.put('I', 1);
map.put('V', 5);
map.put('X', 10);
map.put('L', 50);
map.put('C', 100);
map.put('D', 500);
map.put('M', 1000);
String s = "";
System.out.println(s + " : " + RomanToInt(s));
}
}
~~~~~~~~~~~~
Int 转罗马
class MapNode{
public int key;
public String value;
MapNode(int k, String str){
key = k;
value = str;
}
}
public class IntToRoman {
public static ArrayList
private static String IntToRomans(int input) {
if (input == 0) {
return null;
}
String result = "";
for(MapNode node:map){
int current = node.key;
while (input >= current) {
result += node.value;
input = input - current;
}
}
return result;
}
private static void Init() {
map = new ArrayList
map.add(new MapNode(1000,"M"));
map.add(new MapNode(900,"CM"));
map.add(new MapNode(500,"D"));
map.add(new MapNode(400,"CD"));
map.add(new MapNode(100,"C"));
map.add(new MapNode(90,"XC"));
map.add(new MapNode(50,"L"));
map.add(new MapNode(40,"XL"));
map.add(new MapNode(10,"X"));
map.add(new MapNode(9,"IX"));
map.add(new MapNode(5,"V"));
map.add(new MapNode(4,"IV"));
map.add(new MapNode(1,"I"));
}
public static void main(String[] args) {
Init();
int testInput = 10;
System.out.println(testInput + " : " + IntToRomans(testInput));
}
}