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 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

      题意:

      给出N (1 ~ 3500),用罗马数字表示数字,统计 1 到 N 所有字母的总个数。

 

      思路:

      暴力,一个个列举统计即可。注意罗马数字的表示方式。

 

      AC:

/*    
TASK:preface    
LANG:C++    
ID:sum-g1    
*/
#include<stdio.h>
#include<string.h>
int num[10];

void deal1(int k)
{
    num[1] += (k % 5);
    if(k >= 5 && k != 9) num[2]++;
//除了 4 和 9 要特殊处理
    if(k == 4)
    {
        num[1] -= 3;
        num[2]++;
    }
    if(k == 9)
    {
        num[1] -= 3;
        num[3]++;
    }
}

void deal2(int k)
{
    num[3] += (k % 5);
    if(k >= 5 && k != 9) num[4]++;
    if(k == 4)
    {
        num[3] -= 3;
        num[4]++;
    }
    if(k == 9)
    {
        num[3] -= 3;
        num[5]++;
    }
}

void deal3(int k)
{
    num[5] += (k % 5);
    if(k >= 5 && k != 9) num[6]++;
    if(k == 4)
    {
        num[5] -= 3;
        num[6]++;
    }
    if(k == 9)
    {
        num[5] -= 3;
        num[7]++;
    }
}

void deal4(int k)
{
    num[7] += (k % 5);
}

int main()
{
    int n,len;
    freopen("preface.in","r",stdin);        
    freopen("preface.out","w",stdout);
    memset(num,0,sizeof(num));
    scanf("%d",&n);
    for(int i = 1;i <= n;i++)
    {
        int k = i;
        len = 1;
        while(k)
        {
            if(len == 1)    deal1(k % 10);
            if(len == 2)    deal2(k % 10);
            if(len == 3)    deal3(k % 10);
            if(len == 4)    deal4(k % 10);
            len++;
            k /= 10;
        }
    }
    for(int i = 1;i <= 7;i++)
    {
        if(!num[i]) continue;
        else
        {
            if(i == 1) printf("I ");
            if(i == 2) printf("V ");
            if(i == 3) printf("X ");
            if(i == 4) printf("L ");
            if(i == 5) printf("C ");
            if(i == 6) printf("D ");
            if(i == 7) printf("M ");
            printf("%d\n",num[i]);
        }
    }
    return 0;
}

 

 

 

你可能感兴趣的:(number)