数字比较小,所以题目的难点就转到了,阿拉伯数字向罗马数字转化的过程了。
转化也不难。我直接手算了,值得注意的是8的写法VIII(不是IIX)。
整体来说不难。只要观察出每一位是相互独立的就行。
具体代码如下:
/*
ID: awsd1231
PROG: preface
LANG: C++
*/
#include<iostream>
#include<cstdio>
#include<vector>
#include<map>
using namespace std;
int n;
map<char, int> ans;
const char ansDir[7] = {‘I‘, ‘V‘, ‘X‘, ‘L‘, ‘C‘, ‘D‘, ‘M‘};
struct T {
string one;
string ten;
string hun;
string tho;
T () { one = "*"; }
}num[10];
void init() {// IVX XLC CDM
ans[‘I‘] = ans[‘V‘] = ans[‘X‘] = ans[‘L‘] = ans[‘C‘] = ans[‘D‘] = ans[‘M‘] = 0;
num[1].one = "I"; num[1].ten = "X"; num[1].hun = "C"; num[1].tho = "M";
num[2].one = "II"; num[2].ten = "XX"; num[2].hun = "CC"; num[2].tho = "MM";
num[3].one = "III"; num[3].ten = "XXX"; num[3].hun = "CCC"; num[3].tho = "MMM";
num[4].one = "IV"; num[4].ten = "XL"; num[4].hun = "CD";
num[5].one = "V"; num[5].ten = "L"; num[5].hun = "D";
num[6].one = "VI"; num[6].ten = "LX"; num[6].hun = "DC";
num[7].one = "VII"; num[7].ten = "LXX"; num[7].hun = "DCC";
num[8].one = "VIII"; num[8].ten = "LXXX"; num[8].hun = "DCCC";//!!!!8的正确写法
num[9].one = "IX"; num[9].ten = "XC"; num[9].hun = "CM";
}
void count(int x) {
string ansTmp;
int idx = 1;
while (x) {
int tmp = x % 10;
if (idx == 1) ansTmp += num[tmp].one;
if (idx == 2) ansTmp += num[tmp].ten;
if (idx == 3) ansTmp += num[tmp].hun;
if (idx == 4) ansTmp += num[tmp].tho;
x /= 10;
++idx;
}
int len = ansTmp.length();
for(int i = 0; i != len; ++i) {
++ans[ansTmp[i]];
}
}
int main () {
freopen("preface.in", "r", stdin);
freopen("preface.out", "w", stdout);
scanf("%d", &n);
init();
for (int i = 1; i != n + 1; ++i) {
count(i);
}
for (int i = 0; i != 7; ++i) {
if (ans[ansDir[i]]) cout << ansDir[i] << " " << ans[ansDir[i]] << endl;
}
return 0;
}
时间: 2024-12-27 22:26:57