A certain book's prefaces are numbered in upper case Roman numerals. Traditional Roman numeral values use a single letter to represent a certain subset of decimal numbers. Here is the standard set:
I 1 L 50 M 1000 V 5 C 100 X 10 D 500
As many as three of the same marks that represent 10n may be placed consecutively to form other numbers:
- III is 3
- CCC is 300
Marks that have the value 5x10n are never used consecutively.
Generally (with the exception of the next rule), marks are connected together and written in descending order to form even more numbers:
Sometimes, a mark that represents 10^n is placed before a mark of one of the two next higher values (I before V or X; X before L or C; etc.). In this case, the value of the smaller mark is SUBTRACTED from the mark it precedes:
- IV = 4
- IX = 9
- XL = 40
Compound marks like XD, IC, and XM are not legal, since the smaller mark is too much smaller than the larger one. For XD (wrong for 490), one would use CDXC; for IC (wrong for 99), one would use XCIX; for XM (wrong for 990), one would use CMXC. 90 is expressed XC and not LXL, since L followed by X connotes that successive marks are X or smaller (probably, anyway).
Given N (1 <= N < 3,500), the number of pages in the preface of a book, calculate and print the number of I's, V's, etc. (in order from lowest to highest) required to typeset all the page numbers (in Roman numerals) from 1 through N. Do not print letters that do not appear in the page numbers specified.
If N = 5, then the page numbers are: I, II, III, IV, V. The total number of I's is 7 and the total number of V's is 2.
PROGRAM NAME: preface
INPUT FORMAT
A single line containing the integer N.SAMPLE INPUT (file preface.in)
5
OUTPUT FORMAT
The output lines specify, in ascending order of Roman numeral letters, the letter, a single space, and the number of times that letter appears on preface page numbers. Stop printing letter totals after printing the highest value letter used to form preface numbers in the specified set.SAMPLE OUTPUT (file preface.out)
I 7 V 2
출처: http://train.usaco.org/
N 이 주어질 때, 1 ~ N 의 숫자를 로마 숫자로 표현한다 할 때 각 로마 문자의 개수를 출력하시오.
my solving
c++
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | #include <fstream> #include <iostream> #include <algorithm> using namespace std; int pageNumbers[7]; void calcPageNumber(int number) { int num = number; int quotient = num / 1000; pageNumbers[6] += quotient; num %= 1000; if (num >= 900) { pageNumbers[6]++; pageNumbers[4]++; num %= 900; } quotient = num / 500; pageNumbers[5] += quotient; num %= 500; quotient = num / 100; if (quotient == 4) { pageNumbers[5]++; pageNumbers[4]++; } else pageNumbers[4] += quotient; num %= 100; if (num >= 90) { pageNumbers[4]++; pageNumbers[2]++; num %= 90; } quotient = num / 50; pageNumbers[3] += quotient; num %= 50; quotient = num / 10; if (quotient == 4) { pageNumbers[3]++; pageNumbers[2]++; } else pageNumbers[2] += quotient; num %= 10; if (num >= 9) { pageNumbers[2]++; pageNumbers[0]++; num %= 9; } quotient = num / 5; pageNumbers[1] += quotient; num %= 5; if (num == 4) { pageNumbers[1]++; pageNumbers[0]++; } else pageNumbers[0] += num; } int main() { ifstream fin("preface.in"); ofstream fout("preface.out"); int n; fin >> n; for (int i = 1; i <= n; i++) calcPageNumber(i); for (int i = 0; i < 7; i++) { if (pageNumbers[i] == 0) continue; switch (i) { case 0: fout << "I " << pageNumbers[i] << endl; break; case 1: fout << "V " << pageNumbers[i] << endl; break; case 2: fout << "X " << pageNumbers[i] << endl; break; case 3: fout << "L " << pageNumbers[i] << endl; break; case 4: fout << "C " << pageNumbers[i] << endl; break; case 5: fout << "D " << pageNumbers[i] << endl; break; case 6: fout << "M " << pageNumbers[i] << endl; break; default: break; } } fin.close(); fout.close(); return 0; } | cs |
'Algorithm, Data structure > Solved Algorithmic Problem' 카테고리의 다른 글
USACO 2.2 - Runaround Numbers (0) | 2016.07.08 |
---|---|
USACO 2.2 - Subset Sums (0) | 2016.07.07 |
USACO 2.1 - Hamming Codes (0) | 2016.07.07 |
USACO 2.1 - Healthy Holsteins (0) | 2016.07.06 |
USACO 2.1 - Sorting a Three-Valued Sequence (0) | 2016.07.04 |