[USACO2.2]Preface Numbering 序言页码

来源:互联网 发布:动作特效软件 编辑:程序博客网 时间:2024/06/05 00:48

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


  • Preface Numbering序言页码

    目录

     [隐藏] 
    • 1 描述
    • 2 格式
    • 3 SAMPLE INPUT
    • 4 SAMPLE OUTPUT

    [编辑]描述

    一类书的序言是以罗马数字标页码的。传统罗马数字用单个字母表示特定的数值,以下是标准数字表:

    I 1  V 5  X 10  L 50  C 100  D 500  M 1000 

    最多3个同样的可以表示为10n的数字(I,X,C,M)可以连续放在一起,表示它们的和:

    III=3CCC=300

    可表示为5x10n的字符(V,L,D)从不连续出现。

    除了下一个规则,一般来说,字符以递减的顺序接连出现:

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

    有时,一个可表示为10n的数出现在一个比它大1级或2级的数前(I在V或X前面,X在L或C前面,等等)。在这种情况下,数值等于后面的那个数减去前面的那个数:

    IV = 4IX = 9XL = 40


    一个数 用罗马数字来表示 有且仅有一种 而且不能复合嵌套使用(比如I是1 X是10 有人可能要说 IXL就能表示50-10-1 但是IXL绝对不能用来表达39 ) (那么39用什么来表示呢 XXXIX是唯一 而且正确的选择- -)


    像XD, IC, 和XM这样的表达是非法的,因为前面的数比后面的数小太多。对于XD(490的错误表达),可以写成 CDXC; 对于IC(99的错误表达),可以写成XCIX; 对于XM(990的错误表达),可以写成CMXC。 90 写成 XC 而不是 LXL, 因为 L 后面的 X 意味着后继标记是 X 或者更小 (不管怎样,可能吧)(等同于阿拉伯数字 每位 数字分别表示)。


    给定N(1 <= N < 3,500), 序言的页码数,请统计在第1页到第N页中,有几个I出现,几个V出现,等等 (从小到大的顺序)。不要输出没有出现过的字符。

    比如N = 5, 那么页码数为: I, II, III, IV, V. 总共有7个I出现,2个V出现。

    [编辑]格式

    PROGRAM NAME: preface

    INPUT FORMAT:

    (preface.in)

    一个整数N。

    OUTPUT FORMAT:

    (preface.out)

    每行一个字符和一个数字k,表示这个字符出现了k次。字符必须按数字表中的递增顺序输出。

    [编辑]SAMPLE INPUT

    5

    [编辑]SAMPLE OUTPUT

    I 7V 2



    【题解】

    模拟。一位一位地计算,有点类似进制转换。  阿拉伯数字是a+b*10+c*100+d*1000....  而罗马数字是a*1+b*5+c*10+d*50...... 


    /*ID:luojiny1LANG:C++TASK:preface*/#include<cstdio>int train[7]={1,5,10,50,100,500,1000};int ans[7]={0};int n;char s[8]="IVXLCDM";void search(int num){int len=1;while(num){int a,b,c;if(len==1)a=0,b=1,c=2;if(len==2)a=2,b=3,c=4;if(len==3)a=4,b=5,c=6;if(len==4){ans[6]+=num;break;}int de=num%10;if(de<=3&&de>0)ans[a]+=de;else if(de==4){ans[a]++;ans[b]++;}else if(de==5){ans[b]++;}else if(de>=6&&de<=8){ans[b]++;ans[a]+=de-5;}else if(de==9){ans[a]++;ans[c]++;}num/=10;len++;}}int main(){freopen("preface.in","r",stdin);freopen("preface.out","w",stdout);scanf("%d",&n);for(int i=1;i<=n;i++)search(i);for(int i=0;i<7;i++)if(ans[i])printf("%c %d\n",s[i],ans[i]);return 0;}






  • 原创粉丝点击