hdu 1924 CIVIC DILL MIX 罗马数字和阿拉伯数字之间的转换

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1924

CIVIC DILL MIX

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


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
 



两者都是十进制,每十进制取一位转化就行了。


#include<stdio.h>
#include<string.h>
#include<queue>
#include<map>
#include<string>
#include<iostream>
using namespace std;

map<string,int>my;
map<int,string>my2;

int turn1(string ss1)
{		
	int num1=0;
	int car=0;
	int last=1000000;
	for(int i=0;i<ss1.size();i++)//先出现小的减掉  如IIX  减去2   否则就是加
	{
		string ch="";
		ch+=ss1[i];
		if(my[ch]!=last)
		{
			if(my[ch]<last)
				num1+=car;
			else
				num1-=car;
			car=0;

			car+=my[ch];
		}
		else
			car+=my[ch]; 
		last=my[ch];
	}
	num1+=car;
	return num1;
}

string turn2(int n)
{
		string ans="";

		for(int i=1;n;i*=10)
		{
			int nw=n%10;
			n/=10;
			if(nw<=3||i==1000)
			{
				while(nw--)
				{
					ans=my2[i]+ans;
				}
			}
			else if(nw>=3&&nw<=5)
			{
				int tem=5-nw;
				ans=my2[i*5]+ans;//hou
				while(tem--)
					ans=my2[i]+ans;
				
			}
			else if(nw<=8)
			{
				int tem=nw-5;
				
				while(tem--)
					ans=my2[i]+ans;
				ans=my2[i*5]+ans;
			}
			else
			{
				int tem=10-nw;

				ans=my2[i*10]+ans;
				while(tem--)
					ans=my2[i]+ans;
			} 
		}
		return ans;
}
int main()
{ 
	my["I"]=1;
	my["V"]=5;
	my["X"]=10;
	my["L"]=50;
	my["C"]=100;
	my["D"]=500;
	my["M"]=1000;


	my2[1]="I";
	my2[5]="V";
	my2[10]="X";
	my2[50]="L";
	my2[100]="C";
	my2[500]="D";
	my2[1000]="M";
	string ss1,ss2;
	int n;
	int cas=1;
	while(cin>>n,n)
	{
		int ans=0;
		for(int i=0;i<n;i++)
		{
			cin>>ss1;
			ans+=turn1(ss1);
		}
		
		cout<<"Case "<<turn2(cas++)<<": "<<turn2(ans)<<endl;
	}
	return 0;
}

 








你可能感兴趣的:(暴力)