S002-AC.cc 1.27 KB
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

string roman(int n)
{
	string str;
	char n1, n5, n10;
	if(n < 10)
	{
		n1 = 'I';
		n5 = 'V';
		n10 = 'X';
	}
	else if(n < 100)
	{
		n1 = 'X';
		n5 = 'L';
		n10 = 'C';
		n /= 10;
	}
	else if(n < 1000)
	{
		n1 = 'C';
		n5 = 'D';
		n10 = 'M';
		n /= 100;
	}
	else
	{
		n1 = 'M';
		n /= 1000;
	}
	switch(n)
	{
		case 1:
			return str.append(1,n1);
			break;
		case 2:
			return str.append(2,n1);
			break;
		case 3:
			return str.append(3,n1);
			break;
		case 4:
			str += n1;
			str += n5;
			return str;
			break;
		case 5:
			return str.append(1,n5);
			break;
		case 6:
			str += n5;
			str += n1;
			return str;
			break;
		case 7:
			str += n5;
			str.append(2,n1);
			return str;
			break;
		case 8:
			str += n5;
			str.append(3,n1);
			return str;
			break;
		case 9:
			str += n1;
			str += n10;
			return str;
			break;
	}
}

string toRoman(int n)
{
	string result;
	int digit = 0;
	while(n)
	{
		while(!(n%10))
		{
			digit++;
			n /= 10;
		}
		if(digit)
		{
				result.insert(0,roman((n%10)*pow(10,digit++)));
		}
		else
		{
			result.insert(0,roman(n%10));
			digit++;
		}
		n /= 10;
	}
	return result;
}

int main()
{
	int n;
	while(cin >> n)
	{
		cout << n << " = " << toRoman(n) << endl;
	}
	return 0;
}