• USACO2.2.1Preface 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
      

      题解:想一阵愣是没想出怎么生成罗马数字,然后去Google了一下,找到一个非常简单的生成办法,这个应该很容易懂。(这里还有个1-4999的罗马数字生成器

    • 直接上代码:
    • View Code
       1 /*
       2 ID:spcjv51
       3 PROG:preface
       4 LANG:C
       5 */
       6 #include<stdio.h>
       7 #include<string.h>
       8 int a[7];
       9 char s[7]= {'I','V','X','L','C','D','M'};
      10 void count(char s[])
      11 {
      12     int i;
      13     for(i=0; i<strlen(s); i++)
      14     {
      15         switch(s[i])
      16         {
      17         case 'I':
      18             a[0]++;
      19             break;
      20         case 'V':
      21             a[1]++;
      22             break;
      23         case 'X':
      24             a[2]++;
      25             break;
      26         case 'L':
      27             a[3]++;
      28             break;
      29         case 'C':
      30             a[4]++;
      31             break;
      32         case 'D':
      33             a[5]++;
      34             break;
      35         case 'M':
      36             a[6]++;
      37             break;
      38         }
      39     }
      40 }
      41 int main(void)
      42 {
      43     freopen("preface.in","r",stdin);
      44     freopen("preface.out","w",stdout);
      45     const int nums[13]= {1, 4, 5, 9, 10, 40, 50, 90, 100,400, 500, 900, 1000};
      46     char RomanNums[13][3]= {"I", "IV", "V", "IX", "X", "XL","L", "XC", "C", "CD", "D", "CM", "M"};
      47     int i,j,n,t;
      48     scanf("%d",&n);
      49     memset(a,0,sizeof(a));
      50     for(j=1; j<=n; j++)
      51     {
      52         t=j;
      53         for(i=12; i>=0; i--)
      54             while(t>=nums[i])
      55             {
      56                 t-=nums[i];
      57                 count(RomanNums[i]);
      58             }
      59     }
      60     for(i=0; i<7; i++)
      61         if(a[i]>0)
      62             printf("%c %d\n",s[i],a[i]);
      63 return 0;
      64 }
  • 相关阅读:
    docker使用
    centerpoint模型说明
    python 类:专有方法(魔法方法)
    安装vs2015 windows10 报错
    ubuntu 配置ssh(docker)
    conda、pip如何直接在其他用户下直接运行
    NVIDIA apex安装
    mybatis缓存
    如何简单理解HTTPS
    主板
  • 原文地址:https://www.cnblogs.com/zjbztianya/p/2892191.html
Copyright © 2020-2023  润新知