Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- map<int,string>DecimalToRoman;
- map<string,int>mp;
- string s,R;
- void DtoR(int value)
- {
- DecimalToRoman[1000] = "M";
- DecimalToRoman[900] = "CM";
- DecimalToRoman[500] = "D";
- DecimalToRoman[400] = "CD";
- DecimalToRoman[100] = "C";
- DecimalToRoman[90] = "XC";
- DecimalToRoman[50] = "L";
- DecimalToRoman[40] = "XL";
- DecimalToRoman[10] = "X";
- DecimalToRoman[9] = "IX";
- DecimalToRoman[5] = "V";
- DecimalToRoman[4] = "IV";
- DecimalToRoman[1] = "I";
- int n = value;
- map<int,string>::reverse_iterator it;
- s = "";
- for(it = DecimalToRoman.rbegin(); it!=DecimalToRoman.rend(); it++)
- {
- while(value>=it->first)
- {
- s+=(string) it->second;
- value-=it->first;
- }
- }
- mp[s] = n;
- }
- void init()
- {
- for(int i=1; i<4000; i++)
- {
- DtoR(i);
- }
- return ;
- }
- int main()
- {
- init();
- while(cin>>R)
- {
- if (mp.count(R))
- {
- printf("%d\n", mp[R]);
- }
- else
- {
- puts("This is not a valid number");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment