Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. map<int,string>DecimalToRoman;
  6.  
  7. string R,s;
  8.  
  9. void DtoR(int value)
  10. {
  11.     DecimalToRoman[1000] = "M";
  12.     DecimalToRoman[900] = "CM";
  13.  
  14.     DecimalToRoman[500] = "D";
  15.     DecimalToRoman[400] = "CD";
  16.  
  17.     DecimalToRoman[100] = "C";
  18.     DecimalToRoman[90] = "XC";
  19.  
  20.     DecimalToRoman[50] = "L";
  21.     DecimalToRoman[40] = "XL";
  22.  
  23.     DecimalToRoman[10] = "X";
  24.     DecimalToRoman[9] = "IX";
  25.  
  26.     DecimalToRoman[5] = "V";
  27.     DecimalToRoman[4] = "IV";
  28.  
  29.     DecimalToRoman[1] = "I";
  30.  
  31.     map<int,string>::reverse_iterator it;
  32.  
  33.     s = "";
  34.  
  35.     for(it = DecimalToRoman.rbegin(); it!=DecimalToRoman.rend(); it++)
  36.     {
  37.         while(value>=it->first)
  38.         {
  39.             s+=(string) it->second;
  40.  
  41.             value-=it->first;
  42.         }
  43.     }
  44. }
  45.  
  46. int main()
  47. {
  48.     int len,i,value,tot;
  49.  
  50.     while(cin>>R)
  51.     {
  52.         len = R.length();
  53.  
  54.         value = 0;
  55.  
  56.         tot = 0;
  57.  
  58.         for(i=0; i<len; i++)
  59.         {
  60.             value = value*10+(R[i]-48);
  61.         }
  62.  
  63.         DtoR(value);
  64.  
  65.         len = s.length();
  66.  
  67.         for(i=0;i<len;i++)
  68.         {
  69.             if(s[i]=='I')
  70.             {
  71.                 tot+=1;
  72.             }
  73.             else if(s[i]=='V'||s[i]=='X'||s[i]=='L'||s[i]=='C')
  74.             {
  75.                 tot+=2;
  76.             }
  77.             else if(s[i]=='D')
  78.             {
  79.                 tot+=3;
  80.             }
  81.             else if(s[i]=='M')
  82.             {
  83.                 tot+=4;
  84.             }
  85.         }
  86.  
  87.         cout<<tot<<endl;
  88.     }
  89.  
  90.     return 0;
  91. }