GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GeeksForGeeks/string/RomanToInteger.cpp at master · varunswing/GeeksForGeeks

Expand file tree

/

RomanToInteger.cpp

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
48 lines (42 loc) · 1.11 KB
/

RomanToInteger.cpp

Copy path

File metadata and controls

48 lines (42 loc) · 1.11 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// { Driver Code Starts
// Initial template for C++
// Program to convert Roman Numerals to Numbers
#include <bits/stdc++.h>
using namespace std;
// Returns decimal value of roman numaral
int romanToDecimal(string &);
int main() {
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
cout << romanToDecimal(s) << endl;
}
}// } Driver Code Ends
// User fuunction teemplate for C++
// str given roman number string
// Returns decimal value of roman numaral
int romanToDecimal(string &str) {
// code here
if(!str.size()){
return 0;
}
map<char, int> m;
m.insert(pair<char, int>('I', 1));
m.insert(pair<char, int>('V', 5));
m.insert(pair<char, int>('X', 10));
m.insert(pair<char, int>('L', 50));
m.insert(pair<char, int>('C', 100));
m.insert(pair<char, int>('D', 500));
m.insert(pair<char, int>('M', 1000));
int ans = 0, p = 0;
for(int i=0; i<str.size(); i++){
if(m[str[i]] > m[str[i-1]] && (i-1 >= 0)){
ans += (m[str[i]] - 2*m[str[i-1]]);
}else{
ans += m[str[i]];
}
}
return ans;
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.