用map将所有从1到4000的罗马数字存起来,然后根据输入的字符串输出即可。
/*************************************************************************
> File Name: 759.cpp
> Author: Toy
> Mail: ycsgldy@163.com
> Created Time: 2013年05月30日 星期四 15时47分44秒
************************************************************************/
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <sstream>
#include <fstream>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
using namespace std;
const int INF = 0x7fffffff;
using namespace std;
map < string, int > mymap;
string str;
const int val[13] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
const char roman[13][3] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
string toRoman ( int cnt ) {
string s;
for ( int i = 0; i < 13; ++i ) {
while ( cnt >= val[i] ) {
cnt -= val[i];
s += roman[i][0];
if ( roman[i][1] != '\0' ) s += roman[i][1];
}
}
return s;
}
int main ( ) {
for ( int i = 1; i < 4000; ++i ) {
string s = toRoman ( i );
mymap[s] = i;
}
while ( cin >> str ){
if ( str[0] == '\0' ) cout << "0\n";
else if ( mymap[str] == 0 ) cout << "This is not a valid number\n";
else cout << mymap[str] << endl;
}
return 0;
}