USACO 2.2.1 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 7
V 2

二.题目分析

  本题的关键是要找出表示数字组成的“基”,由题中的综合约束可以得到基本字母表1-I 4-IV 5-V 9-IX 10-X 40-XL 50-L 90-XC 100-C 400-CD 500-D 900-CM 1000-M

对一个数字进行字母表示,则按照从大到小的顺序进行分解就行了,同时统计各字母出现的次数。

三.代码

//字母表 1-I 4-IV 5-V 9-IX 10-X 40-XL 50-L 90-XC 100-C 400-CD 500-D 900-CM 1000-M

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int N,c[26],n,i,j,k;  //采用英语字母表的方式统计字母出现频率,都是为了省事....
    int num[13]={1,4,5,9,10,40,50,90,100,400,500,900,1000};
    char strn[13][3]={"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"};
    char seq[7]={'I','V','X','L','C','D','M'};   //注意题中要求,最后按照此顺序输出
    FILE *in=fopen("preface.in","r"),*out=fopen("preface.out","w");

    if(!in||!out)
    {
        printf("file open error!\n");
        return -1;
    }

    fscanf(in,"%d",&N);
    for(i=0;i<26;i++)
        c[i]=0;

    for(i=1;i<=N;i++)
    {
        n=i;
        for(j=12;j>=0;j--)
        {
            while(n>=num[j])
            {
                n -=num[j];
                for(k=0;strn[j][k]!='\0';k++)
                    c[strn[j][k]-'A']++;
            }
        }
    }
    for(i=0;i<7;i++)
    {
        if(c[seq[i]-'A']>0)
               fprintf(out,"%c %d\n",seq[i],c[seq[i]-'A']);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值