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.
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
Note
I ≤ II ≤ III C ≤ D ≤ I ≤ L ≤ M ≤ V ≤ X IV ≤ IX ≤ V ≤ VI ≤ VII ≤ VIII ≤ X ≤ XI ≤ XII ≤ XIII ≤ XIV
Reference
Author: WU, Zejun
#include<iostream>
using namespace std;
#include<algorithm>
#include<string>
struct s
{
int num;
string eng;//这题用string会方便很多
};
int cmp(s a,s b)
{
return a.eng<b.eng;//因为用的是string,可以这样比较字符串大小
}
string ge[10]={"I","II","III","IV","V","VI","VII","VIII","IX"};
string shi[10]={"X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
string bai[10]={"C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
string qian[4]={"M","MM","MMM"};
int main()
{
int r;
cin>>r;//ZOJ的要求比较严格,头文件是c++的要用cincout
while(r--)
{
s orz[10011];//多组数据,定义在数据开始的时候
int n;
cin>>n;
int i;
for(i=0;i<n;i++)
{
cin>>orz[i].num;
orz[i].eng='\0';//一定要初始化
}
for(i=0;i<n;i++)
{
int tt=orz[i].num;//不可改变原来的值用tt替代
if(tt>=1000)
{
orz[i].eng+=qian[tt/1000-1];//因为是string可以直接+也因此要初始化
tt%=1000;
}
if(tt>=100)
{
orz[i].eng+=bai[tt/100-1];
tt%=100;
}
if(tt>=10)
{
orz[i].eng+=shi[tt/10-1];
tt%=10;
}
if(tt>=1)
{
orz[i].eng+=ge[tt-1];
}
}
sort(orz,orz+n,cmp);//排序
for(i=0;i<n-1;i++)
cout<<orz[i].num<<" ";
cout<<orz[n-1].num<<endl;
}
return 0;
}