http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3501
View Code
#include<iostream>
#include<algorithm>
#include<string.h>
#include<cstdio>
using namespace std;
char a[][15]={"0", "I", "II", "III", "IV", "V", "VI", "VII" , "VIII", "IX"};
char b[][15]={"0", "X", "XX", "XXX", "XL", "L", "LX", "LXX" , "LXXX", "XC"};
char c[][15]={"0", "C", "CC", "CCC", "CD", "D", "DC", "DCC" , "DCCC", "CM"};
char d[][15]={"0", "M", "MM", "MMM"};
struct node
{
char f[15];
char ch[15];
}s[10010];
int cmp(const void *x, const void *y)
{
return strcmp( ((node *)x)->ch , ((node *)y)->ch );
}
int main()
{
int t,i,j;
int n,m;
char ch1[10010];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s",ch1);
strcpy(s[i].f, ch1);
int L=strlen(ch1);
if(L==4)
{
strcpy(s[i].ch, d[ch1[0]-'0']);
if(ch1[1]!='0') strcat(s[i].ch, c[ch1[1]-'0']);
if(ch1[2]!='0') strcat(s[i].ch, b[ch1[2]-'0']);
if(ch1[3]!='0') strcat(s[i].ch, a[ch1[3]-'0']);
}
if(L==3)
{
strcpy(s[i].ch,c[ch1[0]-'0']);
if(ch1[1]!='0') strcat(s[i].ch, b[ch1[1]-'0']);
if(ch1[2]!='0') strcat(s[i].ch , a[ch1[2]-'0']);
}
if(L==2)
{
strcpy(s[i].ch , b[ch1[0]-'0']);
if(ch1[1]!='0') strcat(s[i].ch, a[ch1[1]-'0']);
}
if(L==1)
{
strcpy(s[i].ch,a[ch1[0]-'0']);
}
}
qsort(s,n, sizeof(s[0]), cmp);
for(i=0;i<n;i++)
{
printf("%s",s[i].f);
if(i!=n-1) printf(" ");
}
printf("\n");
}
return 0;
}