Skip to main content

UVa-11616(Roman Numerals)



Bismillahir Rahmanir Rahim

///Author: Tanvir Ahmmad
///CSE,Islamic University,Bangladesh

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<vector>
#include<iterator>
#include <functional> ///sort(arr,arr+n,greater<int>()) for decrement of array
/*every external angle sum=360 degree
  angle find using polygon hand(n) ((n-2)*180)/n*/
///Floor[Log(b)  N] + 1 = the number of digits when any number is represented in base b
using namespace std;
typedef long long ll;

int main()
{
    string str[4009],given;
    string once[]= {"","I","II","III","IV","V","VI","VII","VIII","IX"};
    string twice[]= {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
    string thrice[]= {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
    string fourth[]= {"","M","MM","MMM","MMMM","MMMMM","MMMMMM","MMMMMMM","MMMMMMMM","MMMMMMMMM"};
    for(ll i=1; i<=4000; i++)
    {
        ll num=i,arr[98],k=1;
        str[i]="";
        while(num!=0)
            arr[k++]=num%10,num/=10;
        for(ll j=k-1; j>=1; j--)
        {
            if(j==1)
                str[i]+=once[arr[j]];
            else if(j==2)
                str[i]+=twice[arr[j]];
            else if(j==3)
                str[i]+=thrice[arr[j]];
            else if(j==4)
                str[i]+=fourth[arr[j]];
        }
    }


    while(cin>>given)
    {
        if(given[0]>='0' && given[0]<='9'///Arabic ->-> Roman
        {
            ll given_num=0;
            for(ll i=0; i<given.size(); i++)
                given_num=(given_num*10)+(given[i]-48);
            cout<<str[given_num]<<endl;
        }
        else ///Roman ->-> Arabic
        {
            for(ll i=1; i<=4000; i++)
                if(str[i]==given)
                {
                    cout<<i<<endl;
                    break;
                }
        }
    }
    return 0;
}
///Alhamdulillah

Problem Link:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2663

Comments

Popular posts from this blog

Codeforces Beta Round #53 (A. Square Earth?)

Bismillahir Rahmanir Rahim ///Author: Tanvir Ahmmad ///CSE,Islamic University,Bangladesh #include< iostream > #include< cstdio > #include< algorithm > #include< string > #include< cstring > #include< sstream > #include< cmath > #include< cstring > #include< vector > #include< queue > #include< map > #include< set > #include< stack > #include< vector > #include< iterator > #include   < functional >   ///sort(arr,arr+n,greater<int>()) for decrement of array /*every external angle sum=360 degree   angle find using polygon hand(n) ((n-2)*180)/n*/ ///Floor[Log(b)  N] + 1 = the number of digits when any number is represented in base b using   namespace  std; typedef   long   long ...

AtCoder Beginner Contest 237(D - LR insertion)

Problem solving criteria:stack (https://www.cplusplus.com/reference/stack/stack/) Think about the first input: LRRLR That means  0 ->(L)-> 1 ->(R)-> 2 ->(R)-> 3 ->(L)-> 4 ->(R)-> 5 so we just need to save first R right number  in  an  array (vector) & other number saves  in  a stack. 0 ->(L)-> 1   general  array (vector)       stack( 0 )   top of stack= 0 1 ->(R)-> 2   general  array (vector)= 1      stack( 0 )   top of stack= 0 2 ->(R)-> 3   general  array (vector)= 1 , 2    stack( 0 )   top of stack= 0 3 ->(L)-> 4   general  array (vector)= 1 , 2    stack( 0 , 4 )...

CodeChef --- The Attack of Knight

  Bismillahir Rahmanir Rahim ///Author: Tanvir Ahmmad ///CSE,Islamic University,Bangladesh #include< iostream > #include< cstdio > #include< algorithm > #include< string > #include< cstring > #include< sstream > #include< cmath > #include< cstring > #include< vector > #include< queue > #include< map > #include< set > #include< stack > #include< vector > #include< iterator > #include   < functional >   ///sort(arr,arr+n,greater<int>()) for decrement of array /*every external angle sum=360 degree   angle find using polygon hand(n) ((n-2)*180)/n*/ ///Floor[Log(b)  N] + 1 = the number of digits when any number is represented in base b using   namespace  std; typedef   long  ...