-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP344.cpp
88 lines (82 loc) · 1.55 KB
/
P344.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
void toRomanUpToTen(char *w, int &idx, int n) {
if(n <= 3) {
for(int i = 0; i < n; ++i)
w[idx++] = 'i';
return;
}
if(n == 4) {
w[idx++] = 'i';
w[idx++] = 'v';
return;
}
if(n <= 8) {
w[idx++] = 'v';
for(int i = 6; i <= n; ++i)
w[idx++] = 'i';
return;
}
if(n == 9) {
w[idx++] = 'i';
}
w[idx++] = 'x';
}
void toRomanTens(char *w, int &idx, int n) {
if(n <= 3) {
for(int i = 0; i < n; ++i)
w[idx++] = 'x';
return;
}
if(n == 4) {
w[idx++] = 'x';
w[idx++] = 'l';
return;
}
if(n <= 8) {
w[idx++] = 'l';
for(int i = 6; i <= n; ++i)
w[idx++] = 'x';
return;
}
if(n == 9) {
w[idx++] = 'x';
}
w[idx++] = 'c';
}
void toRoman(char *w, unsigned int n) {
int idx = 0;
toRomanTens(w, idx, n/10);
toRomanUpToTen(w, idx, n%10);
w[idx] = '\0';
}
void countRoman(int *counts, int n) {
char w[20]; // Word.
toRoman(w, n);
for(int i = 0; w[i] != '\0'; ++i) {
++counts[(int)w[i]];
}
}
int main() {
char letters[5] = {'i', 'v', 'x', 'l', 'c'};
int counts[255];
int N;
while(true) {
std::cin >> N;
if(N == 0)
return 0;
// reset counts:
for(int i = 0; i < 5; ++i)
counts[(int)letters[i]] = 0;
// Perform counts:
for(int i = 1; i <= N; ++i)
countRoman(counts, i);
// Output:
std::cout << N << ":";
for(int i = 0; i < 5; ++i) {
if(i != 0)
std::cout << ",";
std::cout << " " << counts[(int)letters[i]] << " " << letters[i];
}
std::cout << std::endl;
}
}