2.2.1 Preface Numbering

数字比较小,所以题目的难点就转到了,阿拉伯数字向罗马数字转化的过程了。

转化也不难。我直接手算了,值得注意的是8的写法VIII(不是IIX)。

整体来说不难。只要观察出每一位是相互独立的就行。

具体代码如下:

  1. /*
  2. ID: awsd1231
  3. PROG: preface
  4. LANG: C++
  5. */
  6. #include<iostream>
  7. #include<cstdio>
  8. #include<vector>
  9. #include<map>
  10. using namespace std;
  11. int n;
  12. map<char, int> ans;
  13. const char ansDir[7] = {‘I‘, ‘V‘, ‘X‘, ‘L‘, ‘C‘, ‘D‘, ‘M‘};
  14. struct T {
  15. string one;
  16. string ten;
  17. string hun;
  18. string tho;
  19. T () { one = "*"; }
  20. }num[10];
  21. void init() {// IVX XLC CDM
  22. ans[‘I‘] = ans[‘V‘] = ans[‘X‘] = ans[‘L‘] = ans[‘C‘] = ans[‘D‘] = ans[‘M‘] = 0;
  23. num[1].one = "I"; num[1].ten = "X"; num[1].hun = "C"; num[1].tho = "M";
  24. num[2].one = "II"; num[2].ten = "XX"; num[2].hun = "CC"; num[2].tho = "MM";
  25. num[3].one = "III"; num[3].ten = "XXX"; num[3].hun = "CCC"; num[3].tho = "MMM";
  26. num[4].one = "IV"; num[4].ten = "XL"; num[4].hun = "CD";
  27. num[5].one = "V"; num[5].ten = "L"; num[5].hun = "D";
  28. num[6].one = "VI"; num[6].ten = "LX"; num[6].hun = "DC";
  29. num[7].one = "VII"; num[7].ten = "LXX"; num[7].hun = "DCC";
  30. num[8].one = "VIII"; num[8].ten = "LXXX"; num[8].hun = "DCCC";//!!!!8的正确写法
  31. num[9].one = "IX"; num[9].ten = "XC"; num[9].hun = "CM";
  32. }
  33. void count(int x) {
  34. string ansTmp;
  35. int idx = 1;
  36. while (x) {
  37. int tmp = x % 10;
  38. if (idx == 1) ansTmp += num[tmp].one;
  39. if (idx == 2) ansTmp += num[tmp].ten;
  40. if (idx == 3) ansTmp += num[tmp].hun;
  41. if (idx == 4) ansTmp += num[tmp].tho;
  42. x /= 10;
  43. ++idx;
  44. }
  45. int len = ansTmp.length();
  46. for(int i = 0; i != len; ++i) {
  47. ++ans[ansTmp[i]];
  48. }
  49. }
  50. int main () {
  51. freopen("preface.in", "r", stdin);
  52. freopen("preface.out", "w", stdout);
  53. scanf("%d", &n);
  54. init();
  55. for (int i = 1; i != n + 1; ++i) {
  56. count(i);
  57. }
  58. for (int i = 0; i != 7; ++i) {
  59. if (ans[ansDir[i]]) cout << ansDir[i] << " " << ans[ansDir[i]] << endl;
  60. }
  61. return 0;
  62. }

来自为知笔记(Wiz)

时间: 2024-08-13 09:35:35

2.2.1 Preface Numbering的相关文章

USACO 2.2 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 5

USACO:2.2.1 Preface Numbering 序言页码

USACO:2.2.1 Preface Numbering 序言页码 一.题目描述 ★Preface Numbering 序言页码 一类书的序言是以罗马数字标页码的.传统罗马数字用单个字母表示特定的数值,一下是标准数字 表: I 1 L 50 M 1000 V 5 C 100 X 10 D 500 最多3 个可以表示为10n 的数字(I,X,C,M)可以连续放在一起,表示它们的和: III=3 CCC=300 可表示为5x10n 的字符(V,L,D)从不连续出现. 除了下一个规则,一般来说,字符

USACO Section 2.2 Preface Numbering

/* ID: lucien23 PROG: preface LANG: C++ */ #include <iostream> #include <fstream> #include <string> #include <map> using namespace std; int main() { ifstream infile("preface.in"); ofstream outfile("preface.out")

USACO Preface Numbering 构造

一开始看到这道题目的时候,感觉好难 还要算出罗马的规则. 但是仔细一看,数据规模很小, n 只给到3500 看完题目给出了几组样例之后就有感觉了 解题方法就是: n的每个十进制数 转换成相应的罗马数字,然后统计每个罗马数字出现的次数即可 还是一道简单的构造题. (以下摘自https://www.byvoid.com/blog/usaco-221preface-numbering/) 转化有以下规则: 1.数较大部分在前,较小部分在后 2.表示10整倍数的字母(I X C M)最多可以累加三次 3

【USACO 2.2】Preface Numbering (找规律)

求 1-n 的所有罗马数字表达中,出现过的每个字母的个数. 分别对每个数的罗马表达式计算每个字母个数. 对于十进制的每一位,都是一样的规则,只是代表的字母不同. 于是我们从最后一位往前考虑,当前位由字母 s[i] 代表 1,字母 s[i+1] 代表 5,s[i+2] 代表 10(在下一次代表1). 每一位考虑完 i+=2; num[i] 为当前位为i对应的 s[i] 的个数,当前位为 4~8 时,s[i+1] 出现 1 次,当前位为 9 时,s[i+2] 出现一次. http://train.u

usaco Preface Numbering

题目算法不难,难的是读懂题意,意思是从1到N的数字转换成罗马数字,然后统计所有数字中的各种字母出现的次数 对于每个数,用贪心的方法转换为罗马数字,然后统计就好了 /* ID: modengd1 PROG: preface LANG: C++ */ #include <iostream> #include <stdio.h> #include <memory.h> #include <string> #include <cstring> using

Preface Numbering

链接 分析:先打表需要用到的罗马数字,然后暴力转换,最后统计一下即可 1 /* 2 PROB:preface 3 ID:wanghan 4 LANG:C++ 5 */ 6 #include "iostream" 7 #include "cstdio" 8 #include "cstring" 9 #include "string" 10 #include "map" 11 using namespace s

USACO Section 2.1: Preface Numbering

看网上的,太琐碎 1 /* 2 ID: yingzho2 3 PROG: preface 4 LANG: C++ 5 */ 6 #include <iostream> 7 #include <fstream> 8 #include <string> 9 #include <map> 10 #include <vector> 11 #include <set> 12 #include <algorithm> 13 #incl

【Preface Numbering】罗马数字

[Problem description] 一类书的序言是以罗马数字标页码的.传统罗马数字用单个字母表示特定的数值,一下是标准数字表: I 1 L 50 M 1000 V 5 C 100 X 10 D 500 最多3个可以表示为10n的数字(I,X,C,M)可以连续放在一起,表示它们的和: III=3 CCC=300 可表示为5x10n的字符(V,L,D)从不连续出现. 除了下一个规则,一般来说,字符以递减的顺序接连出现: CCLXVIII = 100+100+50+10+5+1+1+1 = 2