Usaco 2.2 Preface numbering

Source: Internet
Author: User

Preface numbering

A certain book ' s prefaces is numbered in upper case Roman numerals. Traditional Roman numeral values use a single letter to represent a certain subset of decimal numbers. Here are the standard set:

        I   1     L    M   5     C        X  500 D

As many as three of the same marks that represent 10n could be placed consecutively to form other numbers:

    • III is 3
    • CCC is 300

Marks that has the value 5x10n is never used consecutively.

Generally (with the exception of the next rule), marks is 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, represents 10^n is placed before a mark of one of the and the next higher values (I before V or X; X before L or C; etc.). 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 isn't combined to make another compound mark (e.g., IXL are wrong for 39; XXXIX is correct).

    Compound marks like XD, ICS, and XM are not legal, since the smaller mark is too much smaller than the larger one. For XD (wrong to 490), one would use CDXC; For ICS (wrong for a), one would use XCIX; For XM (wrong for 990), one would use CMXC. 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 This does 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 are 7 and the total number of V's is 2.

    Program Name:prefaceinput Formata single line containing the integer n.sample INPUT (file preface.in)
    5
    Output formatthe output lines specify, in ascending order of Roman numeral letters, the letter, a single space, and the Nu Mber of Times, 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

The main topic, do not want to say what, fried chicken boring topic, give you the representation of Roman numerals, asked you from 1 to n appear several times, according to the weight of the output

Train of thought, no idea, direct simulation

1 /*2 ID:FFFGRDC13 Prob:preface4 lang:c++5 */6#include <cstdio>7#include <iostream>8#include <cstring>9#include <string>Ten using namespacestd; One Charbiao[4][Ten][5]={"","I","II","III","IV","V","VI","VII","VIII","IX", A                      "","X","XX","XXX","XL","L","LX","LXX","LXXX","XC", -                      "","C","CC","CCC","CD","D","DC","DCC","DCCC","CM", -                      "","M","MM","MMM"}; the intcnt[ -]; - inttrback[8]={ -,'I'-'A','V'-'A','X'-'A','L'-'A','C'-'A','D'-'A','M'-'A'}; - intMain () - { +Freopen ("preface.in","R", stdin); -Freopen ("Preface.out","W", stdout); +     intN; Amemset (CNT,0,sizeof(CNT)); atscanf"%d",&n); -      for(intI=1; i<=n;i++) -     { -         intnn=i; -         stringans=""; -         intbitt=0; in          while(NN) -         { to             inttemp=nn%Ten; +ans=biao[bitt][temp]+ans; -bitt++; theNn/=Ten; *         } $         //cout<<ans<<endl;Panax Notoginseng         intlen=ans.length (); -          for(intj=0; j<len;j++) the         { +cnt[ans[j]-'A']++; A         } the     } +      for(intI=1; i<=7; i++) -     { $         if(Cnt[trback[i]]) $printf"%c%d\n", trback[i]+'A', Cnt[trback[i]]); -     } -     return 0; the}

Usaco 2.2 Preface numbering

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.