# 【Tyvj1070】罗马数字

## 描述 Description

I 1 V 5 X 10 L 50 C 100 D 500 M 1000

III=3。CCC=300。

CCLXVIII = 100+100+50+10+5+1+1+1 = 268

IV = 4。IX = 9。XL = 40。

2974

I 4165
V 1486
X 4447
L 1485
C 4465
D 1500
M 3225

Tyvj 1070

## 代码 Code

``````#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
struct roma
{
int s;
char c;
}a[10000];
int i,j,t,n,m,l,r,k,z,y,x;
void tho(int x)
{
int i,j,t;
switch (x)
{
case 1:
case 2:
case 3:
a[1000].s+=x;
break;
}
}
void hun(int x)
{
int i,j,t;
switch (x)
{
case 1:
case 2:
case 3:
a[100].s+=x;
break;
case 4:
a[100].s++;
case 5:
a[500].s++;
break;
case 6:
a[100].s++;
a[500].s++;
break;
case 7:
a[100].s++;
a[100].s++;
a[500].s++;
break;
case 8:
a[100].s++;
a[100].s++;
a[100].s++;
a[500].s++;
break;
case 9:
a[100].s++;
a[1000].s++;
break;
}
}
void ten(int x)
{
int i,j,t;
switch (x)
{
case 1:
case 2:
case 3:
a[10].s+=x;
break;
case 4:
a[10].s++;
case 5:
a[50].s++;
break;
case 6:
a[10].s++;
a[50].s++;
break;
case 7:
a[10].s++;
a[10].s++;
a[50].s++;
break;
case 8:
a[10].s++;
a[10].s++;
a[10].s++;
a[50].s++;
break;
case 9:
a[10].s++;
a[100].s++;
break;
}
}
void one(int x)
{
int i,j,t;
switch (x)
{
case 1:
case 2:
case 3:
a[1].s+=x;
break;
case 4:
a[1].s++;
case 5:
a[5].s++;
break;
case 6:
a[1].s++;
a[5].s++;
break;
case 7:
a[1].s++;
a[1].s++;
a[5].s++;
break;
case 8:
a[1].s++;
a[1].s++;
a[1].s++;
a[5].s++;
break;
case 9:
a[1].s++;
a[10].s++;
break;
}
}
bool comp(roma a,roma b)
{
return a.s>b.s;
}
int main()
{
a[1].c='I';a[5].c='V';a[10].c='X';a[50].c='L';
a[100].c='C';a[500].c='D';a[1000].c='M';
scanf("%d",&n);
for (i=1;i<=n;i++)
{
tho(i/1000);
hun((i/100)%10);
ten((i/10)%10);
one(i%10);
}
for (i=0;i<=1000;i++) if (a[i].s>0) printf("%c %d\n",a[i].c,a[i].s);
return 0;
}``````