标签:
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:
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:
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:
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.
5
I 7 V 2
题目大意,不想说什么了,炒鸡无聊的题目,给你罗马数字的表示规律,问你从1到n出现的字母出现了几次,按照权值排序输出
思路,没什么思路,直接模拟
1 /* 2 ID:fffgrdc1 3 PROB:preface 4 LANG:C++ 5 */ 6 #include<cstdio> 7 #include<iostream> 8 #include<cstring> 9 #include<string> 10 using namespace std; 11 char biao[4][10][5]={"","I","II","III","IV","V","VI","VII","VIII","IX", 12 "","X","XX","XXX","XL","L","LX","LXX","LXXX","XC", 13 "","C","CC","CCC","CD","D","DC","DCC","DCCC","CM", 14 "","M","MM","MMM"}; 15 int cnt[27]; 16 int trback[8]={26,‘I‘-‘A‘,‘V‘-‘A‘,‘X‘-‘A‘,‘L‘-‘A‘,‘C‘-‘A‘,‘D‘-‘A‘,‘M‘-‘A‘}; 17 int main() 18 { 19 freopen("preface.in","r",stdin); 20 freopen("preface.out","w",stdout); 21 int n; 22 memset(cnt,0,sizeof(cnt)); 23 scanf("%d",&n); 24 for(int i=1;i<=n;i++) 25 { 26 int nn=i; 27 string ans=""; 28 int bitt=0; 29 while(nn) 30 { 31 int temp=nn%10; 32 ans=biao[bitt][temp]+ans; 33 bitt++; 34 nn/=10; 35 } 36 //cout<<ans<<endl; 37 int len=ans.length(); 38 for(int j=0;j<len;j++) 39 { 40 cnt[ans[j]-‘A‘]++; 41 } 42 } 43 for(int i=1;i<=7;i++) 44 { 45 if(cnt[trback[i]]) 46 printf("%c %d\n",trback[i]+‘A‘,cnt[trback[i]]); 47 } 48 return 0; 49 }
标签:
原文地址:http://www.cnblogs.com/xuwangzihao/p/5031540.html