Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- map<int,string>DecimalToRoman;
- string R,s;
- 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";
- 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;
- }
- }
- }
- int main()
- {
- int len,i,value,tot;
- while(cin>>R)
- {
- len = R.length();
- value = 0;
- tot = 0;
- for(i=0; i<len; i++)
- {
- value = value*10+(R[i]-48);
- }
- DtoR(value);
- len = s.length();
- for(i=0;i<len;i++)
- {
- if(s[i]=='I')
- {
- tot+=1;
- }
- else if(s[i]=='V'||s[i]=='X'||s[i]=='L'||s[i]=='C')
- {
- tot+=2;
- }
- else if(s[i]=='D')
- {
- tot+=3;
- }
- else if(s[i]=='M')
- {
- tot+=4;
- }
- }
- cout<<tot<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment