UVa 11616

From Algorithmist
Jump to navigation Jump to search

116 - Roman Numerals[edit]

Summary[edit]

Converting from Integer to Roman and Roman to Integers.

Explanation[edit]

The input may be of two kinds. If the input is a Roman number then the output will be its equivalent int value. On the other hand if the input is an integer we have to produce the Roman number equivalent of the input.

Gotchas[edit]

Need to check what is the type of current input


Implementations[edit]

import java.util.HashMap; import java.util.Scanner;

public class UVA_11616 {

public static void main(String[] args) { String [] romans ={"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"}; int [] arabic ={1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000}; HashMap<String, Integer> map =new HashMap<>(); for(int i=0;i<romans.length;i++){ map.put(romans[i], arabic[i]); } Scanner sc = new Scanner(System.in); while(sc.hasNextLine()) { String line = sc.nextLine(); if(line == null || line.isEmpty()) { break; } if(Character.isDigit(line.charAt(0))) { int num = Integer.parseInt(line); String res= ""; int ind =12; while(num>0) { for(; arabic[ind]>num;ind--); num = num -arabic[ind]; res += romans[ind];

} System.out.println(res); } else { int res =0; for(int i=0;i<line.length();i++){ String x = ""; if(i<line.length()-1) { x = line.substring(i, i+2);

} if(map.containsKey(x)) { res+=map.get(x); i++; } else { x =line.substring(i,i+1); res+=map.get(x);

} } System.out.println(res); } } sc.close();

}

}

</syntaxhighlight>


Input[edit]

XXV
4
942
MCMLXXXIII

Output[edit]

25
IV
CMXLII
1983

References[edit]

  1. Reference 1

Categories here, use the form [[Category: Category Name]], see Categories for a list