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.
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.
For each test case, output the n numbers in specified order.
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
1 2 3 100 500 1 50 1000 5 10 4 9 5 6 7 8 10 11 12 13 14
I ≤ II ≤ III C ≤ D ≤ I ≤ L ≤ M ≤ V ≤ X IV ≤ IX ≤ V ≤ VI ≤ VII ≤ VIII ≤ X ≤ XI ≤ XII ≤ XIII ≤ XIV
模拟题,按要求将数字转化为罗马数字,用qsort排序后,再按顺序将其原数字输出即可。
代码如下:
#include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<cmath> using namespace std; struct abc { int num; char x[30]; } a[10002]; void solve(int i) { int t = a[i].num / 1000; //千位转换 memset(a[i].x, 0, sizeof(a[i].x)); if(t) { if(t == 1) strcat(a[i].x, "M"); else if(t == 2) strcat(a[i].x, "MM"); else if(t == 3) strcat(a[i].x, "MMM"); } t = a[i].num %1000 / 100; //百位转换 if(t) { if(t == 1) strcat(a[i].x, "C"); else if(t == 2) strcat(a[i].x, "CC"); else if(t == 3) strcat(a[i].x, "CCC"); else if(t == 4) strcat(a[i].x, "CD"); else if(t == 5) strcat(a[i].x, "D"); else if(t == 6) strcat(a[i].x, "DC"); else if(t == 7) strcat(a[i].x, "DCC"); else if(t == 8) strcat(a[i].x, "DCCC"); else if(t == 9) strcat(a[i].x, "CM"); } t = a[i].num %100 / 10; //十位转换 if(t) { if(t == 1) strcat(a[i].x, "X"); else if(t == 2) strcat(a[i].x, "XX"); else if(t == 3) strcat(a[i].x, "XXX"); else if(t == 4) strcat(a[i].x, "XL"); else if(t == 5) strcat(a[i].x, "L"); else if(t == 6) strcat(a[i].x, "LX"); else if(t == 7) strcat(a[i].x, "LXX"); else if(t == 8) strcat(a[i].x, "LXXX"); else if(t == 9) strcat(a[i].x, "XC"); } t = a[i].num % 10; // 个位转换 { if(t == 1) strcat(a[i].x, "I"); else if(t == 2) strcat(a[i].x, "II"); else if(t == 3) strcat(a[i].x, "III"); else if(t == 4) strcat(a[i].x, "IV"); else if(t == 5) strcat(a[i].x, "V"); else if(t == 6) strcat(a[i].x, "VI"); else if(t == 7) strcat(a[i].x, "VII"); else if(t == 8) strcat(a[i].x, "VIII"); else if(t == 9) strcat(a[i].x, "IX"); } } int st_cmp(const void *s1, const void *s2) { return strcmp((*(abc*)s1).x, (*(abc*)s2).x); } int main() { #ifdef test freopen("in.txt", "r", stdin); #endif int t, n; while(scanf("%d", &t) != EOF) while(t--) { scanf("%d",&n); for(int i = 0; i < n; i++) { scanf("%d", &a[i].num); solve(i); } qsort(a, n, sizeof(a[0]), st_cmp); printf("%d", a[0].num); for(int i = 1; i < n; i++) printf(" %d",a[i].num); puts(""); } return 0; }
代码如下: