Sunday, June 7, 2015

UVa 344 - Roman Digititis

// UVa 344 - Roman Digititis

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

const int roman_value[6] = { 0, 1, 5, 10, 50, 100 };
const char roman_char[6] = { '0', 'i', 'v', 'x', 'l', 'c' };

int main() {
	int roman_count[101][6];
	// roman 1
	roman_count[1][1] = 1;
	for (int j = 2; j <= 5; j++)
		roman_count[1][j] = 0;
	// loop
	for (int i = 2; i <= 100; i++) {
		// init
		for (int j = 1; j <= 5; j++)
			roman_count[i][j] = 0;
		// extract decimal digits
		int c = i / 100;
		int d = i / 10;
		int u = i % 10;
		// break cases
		if (c == 1)
			roman_count[i][5] = 1;
		else {
			if (d == 9) {
				roman_count[i][5]++;
				roman_count[i][3]++;
			} else if (d == 4) {
				roman_count[i][4]++;
				roman_count[i][3]++;
			} else {
				roman_count[i][4] += (d / 5);
				roman_count[i][3] += (d % 5);
			}
			if (u == 9) {
				roman_count[i][3]++;
				roman_count[i][1]++;
			} else if (u == 4) {
				roman_count[i][2]++;
				roman_count[i][1]++;
			} else {
				roman_count[i][2] += (u / 5);
				roman_count[i][1] += (u % 5);
			}
		}
		// accumulate
		for (int j = 1; j <= 5; j++)
			roman_count[i][j] += roman_count[i - 1][j];
	}
	// input
	int n;
	while (cin >> n && n) {
		printf("%d: %d i", n, roman_count[n][1]);
		for (int j = 2; j <= 5; j++)
			printf(", %d %c", roman_count[n][j], roman_char[j]);
		printf("\n");
	}
	return 0;
}

No comments:

Post a Comment