# 4.33 羅馬數字
```
// for 小於4000的數字
#include <stdio.h>
char *toRoman(int n);
int main(){
int n;
scanf("%d", &n);
printf("%s", toRoman(n));
}
char *toRoman(int n){
int y[4];
for (int i=0; i<4; ++i){
y[3-i] = n%10;
n /= 10;
}
//char *x[]={"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"};
char r[] = "MDCLXVI";
static char roman[17];
int k=0;
for (int i=0; i<4; ++i){
if (y[i]<=3)
for (int j=0; j<y[i]; j++)
roman[k++] = r[i*2];
else if (y[i]==4){
roman[k++] = r[i*2];
roman[k++] = r[i*2-1];
}
else if (y[i]<=8){
roman[k++] = r[i*2-1];
for (int j=5; j<y[i]; j++)
roman[k++] = r[i*2];
}
else if (y[i]==9){
roman[k++] = r[i*2];
roman[k++] = r[i*2-2];
}
}
return roman;
}
//http://squall.cs.ntou.edu.tw/cprog/practices/a013_arabic2Roman_4up.pdf
//https://zhuanlan.zhihu.com/p/104273756
void toRoman(int n) {
int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
const char* romans[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
for (int i = 0; i < 13; i++) {
while (n >= values[i]) {
n -= values[i];
printf("%s", romans[i]);
}
}
}
```