ACM The Famous Clock

OpenSoucre發表於2014-04-06

The Famous Clock

時間限制:1000 ms  |  記憶體限制:65535 KB
難度:1
 
描述

Mr. B, Mr. G and Mr. M are now in Warsaw, Poland, for the 2012’s ACM-ICPC World Finals Contest. They’ve decided to take a 5 hours training every day before the contest. Also, they plan to start training at 10:00 each day since the World Final Contest will do so. The scenery in Warsaw is so attractive that Mr. B would always like to take a walk outside for a while after breakfast. However, Mr. B have to go back before training starts, otherwise his teammates will be annoyed. Here is a problem: Mr. B does not have a watch. In order to know the exact time, he has bought a new watch in Warsaw, but all the numbers on that watch are represented in Roman Numerals. Mr. B cannot understand such kind of numbers. Can you translate for him?

 
輸入
Each test case contains a single line indicating a Roman Numerals that to be translated. All the numbers can be found on clocks. That is, each number in the input represents an integer between 1 and 12. Roman Numerals are expressed by strings consisting of uppercase ‘I’, ‘V’ and ‘X’. See the sample input for further information.
輸出
For each test case, display a single line containing a decimal number corresponding to the given Roman Numerals.
樣例輸入
I
II
III
IV
V
VI
VII
VIII
IX
X
XI
XII
樣例輸出
Case 1: 1
Case 2: 2
Case 3: 3
Case 4: 4
Case 5: 5
Case 6: 6
Case 7: 7
Case 8: 8
Case 9: 9
Case 10: 10
Case 11: 11
Case 12: 12

 

#include <iostream>
#include <map>
#include <string>
using namespace std;

int romanToInt(string s){
    int length = s.length();
    if (length < 1) return 0;
    map<int,int> romanMap;
    romanMap['I'] = 1;romanMap['V'] = 5;romanMap['X'] = 10;
    int index = length - 1 , sum = romanMap[s[index]];
    while (--index >= 0) {
        if (romanMap[s[index+1]] >romanMap[s[index]]) sum -= romanMap[s[index]];
        else sum += romanMap[s[index]];
    }
    return sum;
}

int main(){
    int i = 0;
    string str;
    while(cin >> str) cout<<"Case "<<++i<<": "<<romanToInt(str)<<endl;
    return 0;
}