Update H3Q1.java
This commit is contained in:
74
H3Q1.java
74
H3Q1.java
@@ -1,35 +1,23 @@
|
|||||||
// Question 1
|
// Question 1
|
||||||
import java.util.HashMap;
|
// Import the LinkedHashMap and Scanner
|
||||||
import java.util.Map;
|
import java.util.*;
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
// My logic here. We cant have 100, so C is disregarded. Only care about L,X,V,I
|
/* I chose to approach this with an algorithm that has a map with the values in the range
|
||||||
String create_roman(int input) {
|
Then it takes the input, and divides it by the largest number. We will append the roman numeral of that larger
|
||||||
|
number the amount of times that it divides. Then go to the next next largest number, and repeat.
|
||||||
|
Example: 80. 90 does not divide into 80 once, so nothing is appended to the string. 50 does divide into 80 once,
|
||||||
|
so we append 'L'(50) to the return string. Using module, we take the remaining 30. 10 divides into 30 evenly 3 times
|
||||||
|
So 3 'X'(10) are appended. We divide 40 and 9 to account for 'IX' and 'IV'
|
||||||
|
*/
|
||||||
|
static String create_roman(int input) {
|
||||||
|
int divide_count = 0; // Counts the amount of times the number divides into input
|
||||||
|
String roman = ""; // Return string we append to
|
||||||
|
|
||||||
|
// Ordered map that holds the numbers and their roman equivalents
|
||||||
|
Map<Integer, String> test = new LinkedHashMap<>();
|
||||||
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
int input;
|
|
||||||
Scanner scanner = new Scanner(System.in);
|
|
||||||
|
|
||||||
do {
|
|
||||||
System.out.print("Enter an integer between 1 and 90, inclusively: ");
|
|
||||||
input = scanner.nextInt();
|
|
||||||
} while (input < 1 || input > 90); // Ask for input again if not in range of [1,90]
|
|
||||||
|
|
||||||
System.out.println(input);
|
|
||||||
|
|
||||||
int times = 0;
|
|
||||||
|
|
||||||
Map<Integer, String> test = new HashMap<>();
|
|
||||||
test.put(100, "C");
|
|
||||||
test.put(90, "XC");
|
test.put(90, "XC");
|
||||||
test.put(50, "L");
|
test.put(50, "L");
|
||||||
test.put(40, "XL");
|
test.put(40, "XL");
|
||||||
@@ -39,18 +27,34 @@ public class Main {
|
|||||||
test.put(4, "IV");
|
test.put(4, "IV");
|
||||||
test.put(1, "I");
|
test.put(1, "I");
|
||||||
|
|
||||||
int convert = input;
|
// Loop through all the numbers in the map
|
||||||
|
|
||||||
String s = "";
|
|
||||||
|
|
||||||
for(var x : test.entrySet()) {
|
for(var x : test.entrySet()) {
|
||||||
times = convert / x.getKey();
|
// Divide input by the number we are on(starts at 90)
|
||||||
convert %= x.getKey();
|
divide_count = input / x.getKey();
|
||||||
while(times-- > 0) {
|
// Change input value to the remainder
|
||||||
s = s.concat(x.getValue());
|
input %= x.getKey();
|
||||||
|
// This loop appends to return string the amount of times the number was divided in
|
||||||
|
while(divide_count-- > 0) {
|
||||||
|
roman = roman.concat(x.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println("Roman: " + s);
|
return roman;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// Store user input
|
||||||
|
int input;
|
||||||
|
// Scanner object for user input
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
// Ask for user input util user enters something between 1 and 90, inclusively
|
||||||
|
do {
|
||||||
|
System.out.print("Enter an integer between 1 and 90, inclusively: ");
|
||||||
|
input = scanner.nextInt();
|
||||||
|
} while (input < 1 || input > 90); // Ask for input again if not in range of [1,90]
|
||||||
|
|
||||||
|
|
||||||
|
// Print out final result
|
||||||
|
System.out.println("Roman numeral of " + input + ": " + create_roman(input));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user