[Offer收割]编程练习赛25 register

Ended

Participants:399

Verdict:Accepted
Score:100 / 100
Submitted:2017-09-03 12:14:16

Lang:G++

Edit
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
#include<iostream>
#include<cmath>
#include<vector>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
void init(map<char, int>& aux) {
    aux['I'] = 1;
    aux['V'] = 5;
    aux['X'] = 10;
    aux['L'] = 50;
    aux['C'] = 100;
    aux['D'] = 500;
    aux['M'] = 1000;
}
int romanToInt(string s) {
    map<char, int> aux;
    init(aux);
    int len = s.size();
    int sum = 0;
    for (int i = 0; i < len; ++i) {
        if (i<len - 1 && aux[s[i + 1]]>aux[s[i]]) {
            sum += aux[s[i + 1]] - aux[s[i]];
            ++i;
        }
        else
            sum += aux[s[i]];
    }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX