344 – Roman Digititis


An ad-hoc problem.
A brute-force solution that loops over pages 1 through input (n) will suffice. If you’re unfamiliar, the 1-100 list is here.
However, using some deeper analysis, you can find a better solution, but I’ll stick with the trivial one.
C++ implementation:

#include <cstdio>
#include <map>
using namespace std;

#define I i++;
#define V v++;
#define X x++;
#define L l++;
#define C c++;

char sol[] = "%d: %d i, %d v, %d x, %d l, %d c\n";
int n, m, i, v, x, l, c;

int main() {
	while (scanf("%d", &n) == 1 && n++) {
		i = v = x = l = c = 0;
		for (m = 1; m < n; ++m) {
			switch (m % 10) {
			case 1: I break;
			case 2: I I break;
			case 3: I I I break;
			case 4: I V break;
			case 5: V break;
			case 6: V I break;
			case 7: V I I break;
			case 8: V I I I break;
			case 9: I X break;
			}
			switch (m / 10) {
			case 1: X break;
			case 2: X X break;
			case 3: X X X break;
			case 4: X L break;
			case 5: L break;
			case 6: L X break;
			case 7: L X X break;
			case 8: L X X X break;
			case 9: X C break;
			case 10: C break;
			}
		}
		printf(sol, --n, i, v, x, l, c);
	}
	return 0;
}

Oh, good old macros!

993 – Product of digits


A factorization problem.
A straight-forward solution would be dividing the input by the numbers from 9 down to 2 and build the output. If after these divisions the input has value other than 1, then it can’t be solved (there are factors larger than 9).
C++ implementation:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef unsigned long long int64;

int n;
int64 q, d;

int main() {
	int nCase;
	scanf("%d", &nCase);
	while (nCase--) {
		// in
		scanf("%d", &n);
		if (n == 0) {
			printf("10\n");
			continue;
		} else if (n < 10) {
			printf("%d\n", n);
			continue;
		}
		d = (q = 0) + 1;
		// solve
		for (int i = 9; i > 1; --i)
			while (n % i == 0) {
				n /= i;
				q += i * d;
				d *= 10;
			}
		// out
		if (n == 1)
			printf("%lld\n", q);
		else
			printf("-1\n");
	}
	return 0;
}