Guest User

Untitled

a guest
Jun 17th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. // O(n)
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5. int value(char c){
  6. switch(toupper(c)){
  7. case 'I': return 1;
  8. case 'V': return 5;
  9. case 'X': return 10;
  10. case 'L': return 50;
  11. case 'C': return 100;
  12. case 'D': return 500;
  13. case 'M': return 1000;
  14. }
  15. return -1;
  16. }
  17. int solve(string s){
  18. int n=s.length(),ans=0;
  19. for(int i=0;i<n-1;i++){
  20. ans+=(2*(value(s[i])>=value(s[i+1]))-1)*value(s[i]);
  21. }
  22. ans+=value(s[n-1]);
  23. return ans;
  24. }
  25.  
  26. int main(){
  27. int t;
  28. cin>>t;
  29. while(t--){
  30. string s;
  31. cin>>s;
  32. cout<<solve(s)<<'\n';
  33. }
  34. }
  35.  
  36.  
  37. /*
  38. 7
  39. VI
  40. 6
  41. VII
  42. 7
  43. VIII
  44. 8
  45. IX
  46. 9
  47. MDC
  48. 1600
  49. MCD
  50. 1400
  51. MCDL
  52. 1450
  53. */
Add Comment
Please, Sign In to add comment