Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solved problem33 #112

Merged
merged 1 commit into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions javaProblems/problem33/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public String solve(int num) {
int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
StringBuilder result = new StringBuilder();
int i = 0;
while (num > 0) {
if (num >= values[i]) {
int count = num / values[i];
num -= values[i] * count;
for (int j = 0; j < count; j++) {
result.append(symbols[i]);
}
} else {
i++;
}
}
return result.toString();
}
}
25 changes: 25 additions & 0 deletions javaProblems/problem33/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.util.logging.Level;
import java.util.logging.Logger;

public class Test {
public static void main(String[] args) {
Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
Solution solveSolution = new Solution();
String ans;
try {
ans = solveSolution.solve(1994); // Example test case.
} catch (Exception e) {
logger.log(Level.SEVERE, "Runtime error");
System.exit(1);
throw new Error("Runtime Error");
}

// Here implement the logic to compare the answer returned with the actual answers.
if (!ans.equals("MCMXCIV")) {
logger.log(Level.SEVERE, "Wrong solution!");
// If the solution is incorrect, exit with status code 1.
System.exit(1);
}
System.out.print("Testcase passed!");
}
}
35 changes: 35 additions & 0 deletions javaProblems/problem33/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Integer to Roman

## Problem Statement
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number `27` is written as `XXVII`, which is XX + V + II.

`Roman numerals` are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.
Given an integer, convert it to a roman numeral.

```
# Example 1

Input: num = 3
Output: "III"


# Example 2

Input: num = 58
Output: "LVIII"

```