2.2 preface

来源:互联网 发布:最好用的浏览器 知乎 编辑:程序博客网 时间:2024/04/27 17:39

好久没做题了,坚持!

Preface Numbering

A certain book's prefaces are numbered in upper case Roman numerals.Traditional Roman numeral values use a single letter to represent acertain 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 10nmay 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 connectedtogether and written in descending order to form even more numbers:

CCLXVIII = 100+100+50+10+5+1+1+1 = 268

Sometimes, a mark that represents 10^n is placed before a mark ofone 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 fromthe mark it precedes:

  • IV = 4
  • IX = 9
  • XL = 40

This compound mark forms a unit and may not be combined to makeanother compound mark (e.g., IXL is wrong for 39; XXXIX is correct).

Compound marks like XD, IC, and XM are not legal, since thesmaller mark is too much smaller than the larger one. For XD (wrongfor 490), one would use CDXC; for IC (wrong for 99), one would useXCIX; for XM (wrong for 990), one would use CMXC. 90 is expressedXC and not LXL, since L followed by X connotes that successive marksare X or smaller (probably, anyway).

Given N (1 <= N < 3,500), the number of pages in thepreface of a book, calculate and print the number of I's, V's, etc. (inorder from lowest to highest) required to typeset all the page numbers(in Roman numerals) from 1 through N. Do not print letters that do notappear in the page numbers specified.

If N = 5, then the page numbers are: I, II, III, IV, V. The totalnumber 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 appearson preface page numbers. Stop printing letter totals after printingthe highest value letter used to form preface numbers in the specifiedset.

SAMPLE OUTPUT (file preface.out)

I 7
V 2


原创粉丝点击