库你急哇,哈集美马戏特~~
采用贪心算法的话,算法还是比较好写的,就是C语言的字符串处理起来比较麻烦,字符型的二维数组已经忘了,上网回忆了一下字符型二维数组,处理起来就简单多了
题目:
力扣官方题库
上代码(经过线上OJ测试)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * intToRoman(int num){
int arabic[13]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
char roman[13][3]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
int i=0;
char *result=(char *)malloc(sizeof(char)*50);
memset(result,'\0',50);
while(num!=0)
{
for(i=0;i<13;++i)
{
if(num>=arabic[i])
{
num=num-arabic[i];
strcat(result,roman[i]);
break;
}
}
}
return result;
}
int main()
{
char *test=intToRoman(1994);
printf("%s",test);
return 0;
}