博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2.2.1 Preface Numbering
阅读量:5245 次
发布时间:2019-06-14

本文共 3395 字,大约阅读时间需要 11 分钟。

Preface Numbering

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:

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

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

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

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 7V 2
{
ID: makeeca1PROG: prefaceLANG: PASCAL}Program preface;uses math;const num:array[1..4,0..9]of string=(('','I','II','III','IV','V','VI','VII','VIII','IX'),('','X','XX','XXX','XL','L','LX','LXX','LXXX','XC'),('','C','CC','CCC','CD','D','DC','DCC','DCCC','MC'),('','M','MM','MMM','MMMM','','','','',''));var n,i:longint; count:array['A'..'Z'] of longint;procedure deal(x:longint);var i,l:longint;s:string;begin s:=''; l:=1+trunc(ln(x)/ln(10)); for i:=1 to l do begin s:=s+num[i,x mod 10]; x:=x div 10; end; l:=length(s); for i:=1 to l do inc(count[s[i]]);end;begin assign(input,'preface.in');reset(input); assign(output,'preface.out');rewrite(output); readln(n); for i:=1 to n do deal(i); if count['I']>0 then writeln('I ',count['I']); if count['V']>0 then writeln('V ',count['V']); if count['X']>0 then writeln('X ',count['X']); if count['L']>0 then writeln('L ',count['L']); if count['C']>0 then writeln('C ',count['C']); if count['D']>0 then writeln('D ',count['D']); if count['M']>0 then writeln('M ',count['M']); close(output);end.

 

转载于:https://www.cnblogs.com/makeecat/p/3274570.html

你可能感兴趣的文章
HTML <select> 标签
查看>>
类加载机制
查看>>
c# 读/写文件(各种格式)
查看>>
iOS中用UIWebView的loadHTMLString后图片和文字失调解决方法
查看>>
【校招面试 之 C/C++】第24题 C++ STL(六)之Map
查看>>
android基础知识杂记
查看>>
常见浏览器兼容性问题与解决方式
查看>>
Python使用subprocess的Popen要调用系统命令
查看>>
网络编程学习小结
查看>>
常见浏览器兼容性问题与解决方式
查看>>
redis.conf 配置详解
查看>>
thinkphp volist if标签 bug
查看>>
Struts2 Action
查看>>
Strut2------源码下载
查看>>
[LeetCode] 152. Maximum Product Subarray Java
查看>>
Jquery中each的三种遍历方法
查看>>
数据库
查看>>
洛谷 P1967 货车运输(克鲁斯卡尔重构树)
查看>>
D2.Reactjs 操作事件、状态改变、路由
查看>>
ble学习笔记四---------------------控制lcd
查看>>