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
Post a Comment