Roman Order&&字符串处理问题

本文介绍了罗马数字的基本组成规律,并提供了一个将多个罗马数字按字典序排列的算法实现。通过解析每个数字的千位、百位、十位和个位,将其转换为标准罗马数字形式,然后进行排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
OnesIIIIIIIVVVIVIIVIIIIX
TensXXXXXXXLLLXLXXLXXXXC
HundredsCCCCCCCDDDCDCCDCCCCM
ThousandsMMMMMM      

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值