CIVIC DILL MIX

HDU题目连接

这是一道罗马数字转阿拉伯数字的综合练习题。

CIVIC DILL MIX

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 355    Accepted Submission(s): 188


Problem Description
Roman numerals are an ancient numbering system used extensively throughout Europe through the 13th century (where it was eventually replaced by our current positional system). Vestiges of this system still exist today on clock faces, building cornerstones, Super Bowls and Star Wars episodes. The system uses the following 7 symbols:

Symbols I, X, C and M can be repeated as needed (though never more than three times for I, X and C), so that 3 is represented as III, 27 as XXVII and 4865 as MMMMDCCCLXV. The symbols are always written from the highest value to the lowest, but for one exception: if a lower symbol precedes a higher one, it is subtracted from the higher. Thus 4 is written not as IIII but as IV, and 900 is written as CM. The rules for this subtractive behavior are the following:
1. Only I, X and C can be subtracted.
2. These numbers can only appear once in their subtractive versions (e.g., you can’t write 8 as IIX).
3. Each can only come before symbols that are no larger than 10 times their value. Thus we can not write IC for 99 or XD for 490 (these would be XCIX and CDXC, respectively). Note that the first two words in this problem title are invalid Roman numerals, but the third is fine.
Your task for this problem is simple: read in a set of Roman numeral values and output their sum as a Roman numeral.
 

Input
Input will consist of multiple test cases. Each test case starts with a positive integer n indicating the number of values to add. After this will come n values (potentially several on a line), all valid Roman numerals with whitespace only coming between values. A value of n = 0 will indicate end of input. All sums will be less than 5000.
 

Output
For each test case, output the case number and the sum, both as Roman numerals, using the format shown below. Case numbers should start at I.
 

Sample Input
 
  
2 XII MDL 4 I I I I 0
 

Sample Output
 
  
Case I: MDLXII Case II: IV
 

Source
 

Recommend
威士忌   |   We have carefully selected several similar problems for you:   1930  1926  1497  1925  1689 

如果对本文两个算法不懂的,应该先看我前面的博客

阿拉伯数字转罗马数字

罗马数字转阿拉伯数字

AC代码:

#include<iostream>
#include<string>
#include<map>
using namespace std;
map<string, int>m;
void Init()
{
	m["I"] = 1;
	m["V"] = 5;
	m["X"] = 10;
	m["L"] = 50;
	m["C"] = 100;
	m["D"] = 500;
	m["M"] = 1000;
}
int RomanToNumber(string str)
{
	int i;
	string key;
	string ch = str.substr(str.length() - 1);
	int temp = m[ch];
	int temp1 = 0,sum=temp;
	str.erase(str.end() - 1);
	while(str.length())
	{
		key = str.substr(str.length() - 1, 1);
		temp1 = m[key];
		if (temp1 >= temp) sum += temp1;
		else sum -= temp1;
		temp = temp1;
		str.erase(str.end() - 1);
	}
	return sum;
}
string NumberToRoman( int sum)
{
	string str;
	while (sum)
	{
		if (sum >= 1000)
		{
			str.append("M");
			sum -= 1000;
		}
		else if (sum >= 900)
		{
			str.append("C");
			sum += 100;
		}
		else if (sum >= 500)
		{
			str.append("D");
			sum -= 500;
		}
		else if (sum >= 400) {
			str.append("C");
			sum += 100;
		}
		else if (sum >= 100)
		{
			str.append("C");
			sum -= 100;
		}
		else if (sum >= 90)
		{
			str.append("X");
			sum += 10;
		}
		else if (sum >= 50)
		{
			str.append("L");
			sum -= 50;
		}
		else if (sum >= 40) {
			str.append("X");
			sum += 10;
		}
		else if (sum >= 10) {
			str.append("X");
			sum -= 10;
		}
		else if (sum >= 9) {
			str.append("I");
			sum += 1;
		}
		else if (sum >= 5) {
			str.append("V");
			sum -= 5;
		}
		else if (sum >= 4) {
			str.append("I");
			sum += 1;
		}
		else if (sum >= 1) {
			str.append("I");
			sum -= 1;
		}
	}
	return str;
}
int main()
{
	string temp,number,roman,temp1;
	int i, n,sum=0;
	Init();
	map<string, int>::iterator it;
	int c = 0;
	while (cin >> n && n)
	{
		sum = 0;
		for(i=0;i<n;i++)
		{
			cin >> roman;
			sum += RomanToNumber(roman);
		}
		c++;
		temp = NumberToRoman(c);
		temp1 = NumberToRoman(sum);
		cout << "Case " << temp << ": " << temp1 << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40883132/article/details/80798866