Roman numerals are based on seven symbols: I = 1, V = 5, X = 10, L = 50, C = 100, D = 500 and M = 1000.
Symbols are iterated to produce multiples of the decimal (1, 10, 100, 1,000) values, with V, L, D substituted for a multiple of five, and the iteration continuing: I "1", II "2", III "3", V "5", VI "6", VII "7", etc., and the same for other bases: X "10", XX "20", XXX "30", L "50", LXXX "80"; CC "200", DCC "700", etc. At the fourth iteration, a subtractive principle is employed, with the base placed before the higher base: IV for "4", IX for "9", XL for "40", XC for "90", CD for "400", CM for "900".
The basic multiples of Roman numerals thus follow a pattern:
×1 | ×2 | ×3 | ×4 | ×5 | ×6 | ×7 | ×8 | ×9 | |
---|---|---|---|---|---|---|---|---|---|
Ones | I | II | III | IV | V | VI | VII | VIII | IX |
Tens | X | XX | XXX | XL | L | LX | LXX | LXXX | XC |
Hundreds | C | CC | CCC | CD | D | DC | DCC | DCCC | CM |
Thousands | M | MM | MMM |
A practical way to write a Roman number is to consider the modern Arabic numeral system, and separately convert the thousands, hundreds, tens, and ones as given in the chart above. So, for instance, 1234 may be thought of as "one thousand and two hundreds and three tens and four", obtaining M (one thousand) + CC (two hundreds) + XXX (thirty) + IV (four), for MCCXXXIV. Thus eleven is XI (ten and one), 29 is XXIX (twenty and nine), and 2011 is MMXI (two thousand and ten and one). Note that the subtractive principle is not extended beyond the chart: for example, IL is not used for 49, rather this should be written as forty (XL) and nine (IX), or XLIX.
Given a list of numbers, you are to rearrange them so that if we write them as Roman numbers, they are in lexicographical order.
<h4< div="">Input
There are multiple test cases. The first line of input is an integer T ≈ 100 indicating the number of test cases.
Each test case starts with an integer 1 ≤ n ≤ 10000. Then n numbers 0 < ai < 4000.
Output
For each test case, output the n numbers in specified order.
Sample Input
3 3 1 2 3 7 1 5 10 50 100 500 1000 11 4 5 6 7 8 9 10 11 12 13 14
Sample Output
1 2 3 100 500 1 50 1000 5 10 4 9 5 6 7 8 10 11 12 13 14
属于字符串处理的问题。不解释
AC代码:
#include<cstdio> #include<algorithm> #include<string.h> #include<iostream> #include<string> using namespace std; char strHundreds[10][5]={"C","CC","CCC","CD","D","DC","DCC","DCCC","CM"}; char strTens[10][5]={"X","XX","XXX","XL","L","LX","LXX","LXXX","XC"}; char strOnes[10][5]={"I","II","III","IV","V","VI","VII","VIII","IX"}; char strThousands[4][4]={"M","MM","MMM"}; typedef struct { char ch[20]; int id; }Node; Node s[10005]; int s1[10005]; bool operator < (Node const& x,Node const&x1) { return strcmp(x.ch,x1.ch)<0; } int main() { int T; scanf("%d",&T); while(T--) { int n; scanf("%d",&n); for(int i=0;i<n;++i) { int a; char temp[20]="\0"; scanf("%d",&s1[i]); a=s1[i]; if(a/1000) { strcat(temp,strThousands[a/1000-1]); a%=1000; } if(a/100) { strcat(temp,strHundreds[a/100-1]); a%=100; } if(a/10) { strcat(temp,strTens[a/10-1]); a%=10; } if(a) { strcat(temp,strOnes[a-1]); } strcpy(s[i].ch,temp); s[i].id=i; memset(temp,0,sizeof(temp)); } std::sort(s,s+n); for(int i=0;i<n-1;++i) printf("%d ",s1[s[i].id]); printf("%d\n",s1[s[n-1].id]); }return 0; }