码迷,mamicode.com
首页 > 其他好文 > 详细

2.2.1 Preface Numbering

时间:2015-06-05 00:20:17      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

数字比较小,所以题目的难点就转到了,阿拉伯数字向罗马数字转化的过程了。
转化也不难。我直接手算了,值得注意的是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. }





2.2.1 Preface Numbering

标签:

原文地址:http://www.cnblogs.com/liangyongrui/p/4553386.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!