#include #include using namespace std; string ones[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}; string tens[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}; string huns[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}; string lower[999]; // sorted list of strings which come before 'V' int nlower = 0; string upper[999]; // sorted list of string which come at or after 'V'; int nupper = 0; string convert(long val) // convert an integer < 1000 to Roman numerals { string ans = ones[val%10]; val /= 10; ans = tens[val%10] + ans; val /= 10; ans = huns[val%10] + ans; return ans; } void insertLower(string s) // insert into lower sequence in ascending order { int loc = nlower; while (loc > 0 && lower[loc-1] > s) { lower[loc] = lower[loc-1]; loc--; } lower[loc] = s; nlower++; } void insertUpper(string s) // insert into lower sequence in ascending order { int loc = nupper; while (loc > 0 && upper[loc-1] < s) { upper[loc] = upper[loc-1]; loc--; } upper[loc] = s; nupper++; } int find(string s, string a[], int n) { for(int i=0; i> n; for(int i=0; i> val; int numM = val/1000; string s = convert(val % 1000); if (s < "V") { int loc = find(s, lower, nlower)+1; cout << numM*(nlower+1) + loc << endl; // +1 to include M in lower section } else { int loc = find(s, upper, nupper) + 1; cout << -(numM*nupper + loc) << endl; } } }