本文共 1052 字,大约阅读时间需要 3 分钟。
1 #include 2 #include 3 #include 4 using namespace std; 5 6 const int SIZE = 13; 7 const int value[] = { 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000}; 8 const string sign[] = { "I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"}; 9 10 int main()11 {12 map dic;13 for (int n = 1; n < 4000; n++)14 {15 int num = n;16 string Roman = "";17 for(int i = SIZE - 1; i >= 0 && num; i--)18 {19 while(num >= value[i])20 {21 Roman += sign[i];22 num -= value[i];23 }24 }25 dic[Roman] = n;26 }27 28 char s[99];29 while(gets(s))30 {31 if(s[0] == '\0')32 {33 puts("0");34 continue;35 }36 if(dic.count(s)) printf("%d\n", dic[s]);37 else printf("This is not a valid number\n");38 }39 return 0;40 }
转载于:https://www.cnblogs.com/aze-003/p/5143912.html