Verdis Quo+map

16 篇文章 0 订阅
11 篇文章 0 订阅

Verdis Quo

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 616    Accepted Submission(s): 458


Problem Description
The Romans used letters from their Latin alphabet to represent each of the seven numerals in their number system. The list below shows which
letters they used and what numeric value each of those letters represents:

I = 1
V = 5
X = 10
L = 50
C = 100
D = 500
M = 1000

Using these seven numerals, any desired number can be formed by following the two basic additive and subtractive rules. To form a number using
the additive rule the Roman numerals are simply written from left to right in descending order, and the value of each roman numeral is added
together. For example, the number MMCLVII has the value 1000 + 1000 + 100 + 50 + 5 + 1 + 1 = 2157. Using the addition rule alone could lead to
very long strings of letters, so the subtraction rule was invented as a result. Using this rule, a smaller Roman numeral to the left of a larger one is
subtracted from the total. In other words, the number MCMXIV is interpreted as 1000 - 100 + 1000 + 10 - 1 + 5 = 1914.

Over time the Roman number writing system became more standardized and several additional rules were developed. The additional rules used today
are:

1. The I, X, or C Roman numerals may only be repeated up to three times in succession. In other words, the number 4 must be represented as IV
and not as IIII.
2. The V, L, or D numerals may never be repeated in succession, and the M numeral may be repeated as many 2. times as necessary.
3. Only one smaller numeral can be placed to the left of another. For example, the number 18 is represented as XVIII but not as XIIX.
4. Only the I, X, or C can be used as subtractive numerals.
5. A subtractive I can only be used to the left of a V or X. Likewise a X can only appear to the left of a L or C, and a C can only be used to the
left of a D or M. For example, 49 must be written as XLIX and not as IL.

Your goal is to write a program which converts Roman numbers to base 10 integers.
 

Input
The input to this problem will consist of the following:

A line with a single integer "N" (1 ≤ N ≤ 1000), where N indicates how many Roman numbers are to be converted.
A series of N lines of input with each line containing one Roman number. Each Roman number will be in the range of 1 to 10,000 (inclusive)
and will obey all of the rules laid out in the problem's introduction.
 

Output
For each of the N Roman numbers, print the equivalent base 10 integer, one per line.
 

Sample Input
  
  
3 IX MMDCII DXII
 

Sample Output
  
  
9 2602 512
 
/*
题意:将阿拉伯数转化成数学和。
思路:使用stl的map容器,将所有的阿拉伯数进行保存,
然后使用贪心算法,遍历a[],从某个字符开始和它后面的所有字符比较,
如果存在大于它的阿拉伯数,就sum加上这个阿拉伯叔的数学值,
相反则减去。
*/
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<map>
using namespace std;
int main()
{
	int n;
	cin>>n;
	while(n--){
		map<char,int>m;
		char a[1002];
		cin>>a;
		int i,len=strlen(a);
		for(i=0;i<len;i++){
			switch(a[i])
			{
				case 'I':m[a[i]]=1;break;
				case 'V':m[a[i]]=5;break;
				case 'X':m[a[i]]=10;break;
				case 'L':m[a[i]]=50;break;
				case 'C':m[a[i]]=100;break;
				case 'D':m[a[i]]=500;break;
				case 'M':m[a[i]]=1000;break;
			}
		}
		int sum=0;
		for(i=0;i<len;i++){
			for(int j=i+1;j<len;j++)
				if(m[a[i]]<m[a[j]])
					break;
			if(j>=len)
				sum+=m[a[i]];
			else
				sum-=m[a[i]];
		}
		cout<<sum<<endl;
	}
	return 0;
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值