1 #include <cstdio> 2 #include <string> 3 #include <map> 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<string, int> 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 }
时间: 2024-12-21 16:21:47