Algorithm


Problem Name: beecrowd | 1960

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1960

Roman Numerals for Page Numbers

 

By M.C. Pinto, UNILA BR Brazil

Timelimit: 1

The ECI (Editio Chronica Incredibilis or Amazing Chronicles Editors) is very traditional when numbering the pages of its books. It always uses the Roman numerals for that. And its books never have more than 999 pages. When necessary, the books are split into volumes.

You must write a program that, given an arabic number, show its equivalent in roman numerals.

Remember that I stands for 1, V for 5, X for 10, L for 50, C for 100, D for 500 and M stands for 1000.

 

Input

 

The input is a positive integer number N (0 < N < 1000).

 

Output

 

The output is the number N written as Roman numerals in a single line. Always use uppercase letters.

 

 

 

Input Samples Output Samples

666

DCLXVI

 

 

 

83

LXXXIII

 

 

 

999

CMXCIX

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n , temp;
    string s;

    cin >> s;
    int len = s.length();
    if(len==3)
    {
        if(s[0]=='1') printf("C");
        else if(s[0]=='2') printf("CC");
        else if(s[0]=='3') printf("CCC");
        else if(s[0]=='4') printf("CD");
        else if(s[0]=='5') printf("D");
        else if(s[0]=='6') printf("DC");
        else if(s[0]=='7') printf("DCC");
        else if(s[0]=='8') printf("DCCC");
        else if(s[0]=='9') printf("CM");
        if(s[1]=='1') printf("X");
        else if(s[1]=='2') printf("XX");
        else if(s[1]=='3') printf("XXX");
        else if(s[1]=='4') printf("XL");
        else if(s[1]=='5') printf("L");
        else if(s[1]=='6') printf("LX");
        else if(s[1]=='7') printf("LXX");
        else if(s[1]=='8') printf("LXXX");
        else if(s[1]=='9') printf("XC");
        if(s[2]=='1') printf("I");
        else if(s[2]=='2') printf("II");
        else if(s[2]=='3') printf("III");
        else if(s[2]=='4') printf("IV");
        else if(s[2]=='5') printf("V");
        else if(s[2]=='6') printf("VI");
        else if(s[2]=='7') printf("VII");
        else if(s[2]=='8') printf("VIII");
        else if(s[2]=='9') printf("IX");
    }
    else if(len==2)
    {
        if(s[0]=='1') printf("X");
        else if(s[0]=='2') printf("XX");
        else if(s[0]=='3') printf("XXX");
        else if(s[0]=='4') printf("XL");
        else if(s[0]=='5') printf("L");
        else if(s[0]=='6') printf("LX");
        else if(s[0]=='7') printf("LXX");
        else if(s[0]=='8') printf("LXXX");
        else if(s[0]=='9') printf("XC");
        if(s[1]=='1') printf("I");
        else if(s[1]=='2') printf("II");
        else if(s[1]=='3') printf("III");
        else if(s[1]=='4') printf("IV");
        else if(s[1]=='5') printf("V");
        else if(s[1]=='6') printf("VI");
        else if(s[1]=='7') printf("VII");
        else if(s[1]=='8') printf("VIII");
        else if(s[1]=='9') printf("Is");
    }
    else if(len==1)
    {
        if(s[0]=='1') printf("I");
        else if(s[0]=='2') printf("II");
        else if(s[0]=='3') printf("III");
        else if(s[0]=='4') printf("IV");
        else if(s[0]=='5') printf("V");
        else if(s[0]=='6') printf("VI");
        else if(s[0]=='7') printf("VII");
        else if(s[0]=='8') printf("VIII");
        else if(s[0]=='9') printf("IX");
    }
    cout << endl;

    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
666

Output

x
+
cmd
DCLXVI

#2 Code Example with Javascript Programming

Code - Javascript Programming


var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');
let numero = parseInt(lines.shift());

const arabicos = [900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
const romanos = ['CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];

let i = 0;
let resultado = '';

while (numero > 0) {
  if (numero >= arabicos[i]) {
    numero -= arabicos[i];
    resultado += romanos[i];
  } else {
    i++;
  }
}

console.log(resultado);
Copy The Code & Try With Live Editor

Input

x
+
cmd
666

Output

x
+
cmd
DCLXVI
Advertisements

Demonstration


Previous
#1959 Beecrowd Online Judge Solution 1959 Regular Simple Polygons Solution in C++, Java, Js and Python
Next
#1961 Beecrowd Online Judge Solution 1961 Jumping Frog Solution in C++, Java, Js and Python