Problem Links:
UVa12397,Problem:
Problem A: Roman Numerals |
We would like to build Roman numerals with matches. As you know, Roman numerals are based on the following seven characters: I, V, X, L, C, D, M. Here we introduce the LUSIVERS font, in which the respective characters look like this:

Write a program that counts the number of matches used to build Roman numerals in the LUSIVERS font. This number is exactly the total number of "match heads" in the characters. For instance, to make the number 14 (=XIV), five matches are used.

You must follow the "standard modern Roman numerals" (as shown on the Wikipedia page). Expressions like IC or IIII are not allowed.
Input
Input contains multiple lines, each giving a value of N (1 ≤ N ≤ 3999).
Output
For each test case, output the number of matches required to build the number N in Roman numerals.
Sample Input
14 2011
Sample Output
5 11
Solution:
Source Code:
/** UVa 12397, Roman Numerals.
* Solution:
* Ad-hoc implementation.
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
class Main {
private static int[] nums = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9,
5, 4, 1 };
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String strLine;
while ((strLine = br.readLine()) != null) {
int N = Integer.parseInt(strLine);
int count = 0;
for (int i = 0; i < nums.length; i++) {
while (N >= nums[i]) {
N -= nums[i];
count += getMatches(nums[i]);
}
}
System.out.println(count);
}
}
/**
* @return the number of corresponding matches required for the number.
*
* */
public static int getMatches(int number) {
switch (number) {
case 1000:
return 4;
case 900:
return 6;
case 500:
return 3;
case 400:
return 5;
case 100:
return 2;
case 90:
return 4;
case 50:
return 2;
case 40:
return 4;
case 10:
return 2;
case 9:
return 3;
case 5:
return 2;
case 4:
return 3;
case 1:
return 1;
default:
return 1;
}
}
}
No comments :
Post a Comment