首页 > 代码库 > USACO Section 2.2 Preface Numbering

USACO Section 2.2 Preface Numbering

/*
ID: lucien23
PROG: preface
LANG: C++
*/

#include <iostream>
#include <fstream>
#include <string>
#include <map>
using namespace std;

int main()
{
	ifstream infile("preface.in");
	ofstream outfile("preface.out");
	if(!infile || !outfile)
	{
		cout << "file operation failure!" << endl;
		return -1;
	}

	int N;
	infile >> N;

	map<int, string> romans;
	romans.insert(pair<int, string>(1, "I"));
	romans.insert(pair<int, string>(2, "II"));
	romans.insert(pair<int, string>(3, "III"));
	romans.insert(pair<int, string>(4, "IV"));
	romans.insert(pair<int, string>(5, "V"));
	romans.insert(pair<int, string>(6, "VI"));
	romans.insert(pair<int, string>(7, "VII"));
	romans.insert(pair<int, string>(8, "VIII"));
	romans.insert(pair<int, string>(9, "IX"));

	romans.insert(pair<int, string>(10, "X"));
	romans.insert(pair<int, string>(20, "XX"));
	romans.insert(pair<int, string>(30, "XXX"));
	romans.insert(pair<int, string>(40, "XL"));
	romans.insert(pair<int, string>(50, "L"));
	romans.insert(pair<int, string>(60, "LX"));
	romans.insert(pair<int, string>(70, "LXX"));
	romans.insert(pair<int, string>(80, "LXXX"));
	romans.insert(pair<int, string>(90, "XC"));

	romans.insert(pair<int, string>(100, "C"));
	romans.insert(pair<int, string>(200, "CC"));
	romans.insert(pair<int, string>(300, "CCC"));
	romans.insert(pair<int, string>(400, "CD"));
	romans.insert(pair<int, string>(500, "D"));
	romans.insert(pair<int, string>(600, "DC"));
	romans.insert(pair<int, string>(700, "DCC"));
	romans.insert(pair<int, string>(800, "DCCC"));
	romans.insert(pair<int, string>(900, "CM"));

	romans.insert(pair<int, string>(1000, "M"));
	romans.insert(pair<int, string>(2000, "MM"));
	romans.insert(pair<int, string>(3000, "MMM"));

	map<char, int> letters;
	letters['I'] = 0;
	letters['V'] = 0;
	letters['X'] = 0;
	letters['L'] = 0;
	letters['C'] = 0;
	letters['D'] = 0;
	letters['M'] = 0;
	string strBase = "IVXLCDM";

	for (int i=1; i<=N; i++)
	{
		int temp = i;
		string str = "";
		if (temp >= 1000)
		{
			str += romans.at(temp / 1000 * 1000);
			temp %= 1000;
		}

		if (temp >= 100)
		{
			str += romans.at(temp / 100 * 100);
			temp %= 100;
		}

		if (temp >= 10)
		{
			str += romans.at(temp / 10 * 10);
			temp %= 10;
		}
		
		if (temp >= 1)
		{
			str += romans.at(temp);
		}

		int len = str.size();
		for (int j=0; j<len; j++)
		{
			int count = letters.at(str[j]);
			letters[str[j]] = count + 1;
		}
	}

	int len = strBase.size();
	for (int i=0; i<len; i++)
	{
		int count = letters.at(strBase[i]);
		if (count > 0)
		{
			outfile << strBase[i] << " " << count << endl;
		}
	}

	return 0;
}