UVa 759-The Return of the Roman Empire

用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;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值