USACO 2.2.1 Preface Numbering

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

题意:输入一个阿拉伯数字N,从1~N转化为传统罗马数字后,计算这些数字中I L M V C X D各有多少个。

思路:先预处理一下,开一个数组记录一下1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90,100,200,300,400,500,600,700,800,900,1000,2000,3000的写法,其他的1~3500的数字都可以由这些数字相加得到,比如:3247=3000+200+40+7而得到。

源代码:

/*
ID: supersnow0622
PROG: preface
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
string str[3500];
int main() {
    ofstream fout ("preface.out");
    ifstream fin ("preface.in");
    int N;
    fin>>N;
    int i=0,v=0,x=0,l=0,c=0,d=0,m=0;
    str[1]="I";str[2]="II";str[3]="III";str[4]="IV";str[5]="V";str[6]="VI";
    str[7]="VII";str[8]="VIII";str[9]="IX";
    str[10]="X";str[20]="XX";str[30]="XXX";str[40]="XL";str[50]="L";str[60]="LX";
    str[70]="LXX";str[80]="LXXX";str[90]="XC";
    str[100]="C";str[200]="CC";str[300]="CCC";str[400]="CD";str[500]="D";str[600]="DC";
    str[700]="DCC";str[800]="DCCC";str[900]="CM";
    str[1000]="M";str[2000]="MM";str[3000]="MMM";
    int count,temp,n;
    for(int b=1;b<=N;b++)
    {
       string s="";
       count=1;
       n=b;
       while(n!=0)
       {
        temp=n%10*count;
        s+=str[temp];
        n/=10;
        count*=10;
       }
      for(int a=0;a<s.length();a++)
      {
         if(s[a]=='I')
           i++;
         else if(s[a]=='V')
           v++;
         else if(s[a]=='X')
           x++;
         else if(s[a]=='L')
           l++;
         else if(s[a]=='C')
           c++;
         else if(s[a]=='D')
           d++;
         else if(s[a]=='M')
           m++;
      }
    }
    if(i!=0)
    fout<<"I "<<i<<endl;
    if(v!=0)
    fout<<"V "<<v<<endl;
    if(x!=0)
    fout<<"X "<<x<<endl;
    if(l!=0)
    fout<<"L "<<l<<endl;
    if(c!=0)
    fout<<"C "<<c<<endl;
    if(d!=0)
    fout<<"D "<<d<<endl;
    if(m!=0)
    fout<<"M "<<m<<endl;
    return 0;
}



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值