一道囧的模拟题,可以这样做,赞。
大意是求出从1~n的罗马数字的各个字母的统计。
I, II, III, IV, V, VI, VII, VIII, IX 1~9
X, XX, XXX, XL, L, LX, LXX, LXXX, XC 10~90
C, CC, CCC, CD, D, DC, DCC, DCCC, CM 100~900
M, MM, MMM 1000~3000
然后用这些数字组合。
通过这题算是认识罗马数字了。
1 /*
2 ID: litstrong
3 PROG: preface
4 LANG: C++
5 */
6
7 #include < iostream >
8 #include < string >
9 #include < algorithm >
10 #include < string .h >
11 #include < vector >
12 #include < math.h >
13 #include < map >
14 #include < time.h >
15 #include < queue >
16 #include < set >
17 using namespace std;
18
19 /*
20 I 1 L 50 M 1000
21 V 5 C 100
22 X 10 D 500
23 I, II, III, IV, V, VI, VII, VIII, IX 1~9
24 X, XX, XXX, XL, L, LX, LXX, LXXX, XC 10~90
25 C, CC, CCC, CD, D, DC, DCC, DCCC, CM 100~900
26 M, MM, MMM 1000~3000
27 */
28 char a[ 10 ][ 10 ] = { " I " , " II " , " III " , " IV " , " V " , " VI " , " VII " , " VIII " , " IX " };
29 char b[ 10 ][ 10 ] = { " X " , " XX " , " XXX " , " XL " , " L " , " LX " , " LXX " , " LXXX " , " XC " };
30 char c[ 10 ][ 10 ] = { " C " , " CC " , " CCC " , " CD " , " D " , " DC " , " DCC " , " DCCC " , " CM " };
31 char d[ 10 ][ 10 ] = { " M " , " MM " , " MMM " };
32 char mo[ 10 ] = " IVXLCDM " ;
33
34 int n;
35 map < char , int > ci;
36
37 void compute( char * ch)
38 {
39 int len = strlen(ch);
40 for ( int i = 0 ; i < len; i ++ )
41 {
42 ci[ch[i]] ++ ;
43 }
44 }
45
46 void cnt( int val)
47 {
48 int t = 0 ;
49 while (val)
50 {
51 int tt = val % 10 ;
52 val /= 10 ;
53 t ++ ;
54 if (tt == 0 ) continue ;
55 if (t == 1 ) compute(a[tt - 1 ]);
56 else if (t == 2 ) compute(b[tt - 1 ]);
57 else if (t == 3 ) compute(c[tt - 1 ]);
58 else if (t == 4 ) compute(d[tt - 1 ]);
59 }
60 }
61
62 void go()
63 {
64 for ( int i = 1 ; i <= n; i ++ )
65 cnt(i);
66 int len = strlen(mo);
67 for ( int i = 0 ; i < len; i ++ )
68 if (ci[mo[i]] > 0 )
69 printf( " %c %d\n " , mo[i], ci[mo[i]]);
70 }
71
72 void ready()
73 {
74 for ( int i = 0 ; i < strlen(mo); i ++ )
75 {
76 ci[mo[i]] = 0 ;
77 }
78 }
79
80 int main()
81 {
82 freopen( " preface.in " , " r " , stdin);
83 freopen( " preface.out " , " w " , stdout);
84
85 scanf( " %d " , & n);
86 ready();
87 go();
88 }