Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // lab1 zad2, ova e samo pocetok, vamos VAMOOOOOS
- import java.util.Scanner;
- import java.util.stream.IntStream;
- public class RomanConverterTest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = scanner.nextInt();
- IntStream.range(0, n)
- .forEach(x -> System.out.println(RomanConverter.toRoman(scanner.nextInt())));
- scanner.close();
- }
- }
- class RomanConverter {
- /**
- * Roman to decimal converter
- *
- * @param n number in decimal format
- * @return string representation of the number in Roman numeral
- */
- public static String toRoman(int n) {
- // your solution here
- StringBuilder roman_representation= new StringBuilder("");
- while(n>=1000){
- roman_representation.append("M");
- n-=1000;
- }
- while(n>=500){
- if((n/100)%10==9){
- roman_representation.append("CM");
- n-=900;
- }
- else{
- roman_representation.append("D");
- n-=500;
- }
- }
- while(n>=100){
- if((n/100)%10==4){
- roman_representation.append("CD");
- n-=400;
- }
- else{
- roman_representation.append("C");
- n-=100;
- }
- }
- while(n>=50){
- if((n/10)%10==9){
- roman_representation.append("XC");
- n-=90;
- }
- else{
- roman_representation.append("L");
- n-=50;
- }
- }
- while(n>=10){
- if((n/10)%10==4){
- roman_representation.append("XL");
- n-=40;
- }
- else{
- roman_representation.append("X");
- n-=10;
- }
- }
- while(n>=5){
- if(n%10==9){
- roman_representation.append("IX");
- n-=9;
- }
- else{
- roman_representation.append("V");
- n-=5;
- }
- }
- while(n>=1){
- if(n%10==4){
- roman_representation.append("IV");
- n-=4;
- }
- else{
- roman_representation.append("I");
- n-=1;
- }
- }
- return roman_representation.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement