1000ms 65536K 
给定一个整数 numnum,将整数转换成罗马数字。

如 1,2,3,4,51,2,3,4,5 对应的罗马数字分别为I,II,III,IV,V等,更详细的说明见此 链接。

输入格式 
第一行输入一个整数 num(1 leq num leq 3999)num(1≤num≤3999)。

输出格式 
输出 numnum 对应的罗马数字。

样例输入 
123 
样例输出 
CXXIII

 

 1 #include<stdio.h>
 2 #include<malloc.h>
 3 #define N 30
 4 int thousand,hunderd,ten,one,i = 0,j = 0;
 5 char* str;
 6 char Roma[8] = {'I','V','X','L','C','D','M'};
 7 char* calculate(int X);
 8 void SpecialCalculate(int num,int flag);
 9 int main(void)
10 {
11     int num;
12     str = (char*)malloc(sizeof(char)*N);
13     scanf("%d",&num);
14     printf("%s",calculate(num));
15     return 0;
16 }
17 char* calculate(int X)
18 {
19 
20     thousand = X/1000;
21     hunderd = X%1000/100;
22     ten = X%100/10;
23     one = X%10;
24     //处理千位数
25     if(thousand != 0)
26         for(j = 0;j < thousand;j++)
27             str[i++] = Roma[6];
28     if(hunderd != 0)
29     {
30         //3以下的直接循环输出  4则特别输出,5到8循环输出,9特殊
31         SpecialCalculate(hunderd,0);
32     }
33     if(ten != 0)
34     {
35         SpecialCalculate(ten,1);
36     }
37     if(one != 0)
38         SpecialCalculate(one,2);
39     str[i] = '';
40     return str;
41 }
42 void SpecialCalculate(int num,int flag)
43 {
44     int gap = flag*2;
45     if(num <= 3)
46             for(j = 0;j < num;j++)
47                 str[i++] = Roma[4-gap];
48         else if(4 == num)
49         {
50             str[i++] = Roma[4-gap];
51             str[i++] = Roma[5-gap];
52         }
53         else if(9 == num)
54         {         
55             str[i++] = Roma[4-gap];
56             str[i++] = Roma[6-gap];
57         }
58         else
59         {
60             str[i++] = Roma[5-gap];
61             for(j = 0;j<num - 5;j++)
62                 str[i++] = Roma[4-gap];
63         }
64 }

 

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!