Description
Prof. Hachioji has devised a new numeral system of integral numbers with four lowercase letters "m", "c", "x", "i" and with eight digits "2", "3", "4", "5", "6", "7", "8", "9". He doesn't use digit "0" nor digit "1" in this system.
The letters "m", "c", "x" and "i" correspond to 1000, 100, 10 and 1, respectively, and the digits "2", ...,"9" correspond to 2, ..., 9, respectively. This system has nothing to do with the Roman numeral system.
For example, character strings
"5m2c3x4i", "m2c4i" and "5m2c3x"
correspond to the integral numbers 5234 (=5*1000+2*100+3*10+4*1), 1204 (=1000+2*100+4*1), and 5230 (=5*1000+2*100+3*10), respectively. The parts of strings in the above example, "5m", "2c", "3x" and "4i" represent 5000 (=5*1000), 200 (=2*100), 30 (=3*10) and 4 (=4*1), respectively.
Each of the letters "m", "c", "x" and "i" may be prefixed by one of the digits "2", "3", ..., "9". In that case, the prefix digit and the letter are regarded as a pair. A pair that consists of a prefix digit and a letter corresponds to an integer that is equal to the original value of the letter multiplied by the value of the prefix digit.
For each letter "m", "c", "x" and "i", the number of its occurrence in a string is at most one. When it has a prefix digit, it should appear together with the prefix digit. The letters "m", "c", "x" and "i" must appear in this order, from left to right. Moreover, when a digit exists in a string, it should appear as the prefix digit of the following letter. Each letter may be omitted in a string, but the whole string must not be empty. A string made in this manner is called an MCXI-string.
An MCXI-string corresponds to a positive integer that is the sum of the values of the letters and those of the pairs contained in it as mentioned above. The positive integer corresponding to an MCXI-string is called its MCXI-value. Moreover, given an integer from 1 to 9999, there is a unique MCXI-string whose MCXI-value is equal to the given integer. For example, the MCXI-value of an MCXI-string "m2c4i" is 1204 that is equal to 1000 + 2*100 + 4*1. There are no MCXI-strings but "m2c4i" that correspond to 1204. Note that strings "1m2c4i", "mcc4i", "m2c0x4i", and "2cm4i" are not valid MCXI-strings. The reasons are use of "1", multiple occurrences of "c", use of "0", and the wrong order of "c" and "m", respectively.
Your job is to write a program for Prof. Hachioji that reads two MCXI-strings, computes the sum of their MCXI-values, and prints the MCXI-string corresponding to the result.Input
The input is as follows. The first line contains a positive integer n (<= 500) that indicates the number of the following lines. The k+1 th line is the specification of the k th computation (k=1, ..., n).
n
specification1
specification2
...
specificationn
Each specification is described in a line:
MCXI-string1 MCXI-string2
The two MCXI-strings are separated by a space.
You may assume that the sum of the two MCXI-values of the two MCXI-strings in each specification is less than or equal to 9999.Output
For each specification, your program should print an MCXI-string in a line. Its MCXI-value should be the sum of the two MCXI-values of the MCXI-strings in the specification. No other characters should appear in the output.
Sample Input
10 xi x9i i 9i c2x2i 4c8x8i m2ci 4m7c9x8i 9c9x9i i i 9m9c9x8i m i i m m9i i 9m8c7xi c2x8i
Sample Output
3x x 6cx 5m9c9x9i m 9m9c9x9i mi mi mx 9m9c9x9i
题目大意:
小写字母m,c,x,i分别代表1000,100,10,1。剩下的就是运算了,每个字母的前面(暂且称之为系数),不存在0或者1这两种情况。
输入样例解释:10是testcase 然后输入 xi x9i 输出3x
x和i前没有系数,则xi代表的是数字10 + 1 = 11,故x9i代表的是10 + 9 × 1 = 19.二者之和为30,用题目所描述的规则来输出。则答案为3x:3 ×10
Java AC代码
import java.io.BufferedInputStream;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(new BufferedInputStream(System.in));
if (sc.hasNext())
{
int n = sc.nextInt();
sc.nextLine();
for (int i = 0; i < n; i++)
{
String[] ss = sc.nextLine().trim().split(" ");
char[] a = ss[0].toCharArray();
char[] b = ss[1].toCharArray();
int[] va = {0, 0, 0, 0};
int[] vb = {0, 0, 0, 0};
int[] vc = new int[4];
for (int j = 0; j < a.length; j++)
{
if (a[j] - '0' <= 9 && a[j] - '0' >= 2)
{
j++; va[getIndex(a[j])] = a[j - 1] - '0';
}
else
{
va[getIndex(a[j])] = 1;
}
}
for (int j = 0; j < b.length; j++)
{
if (b[j] - '0' <= 9 && b[j] - '0' >= 2)
{
j++; vb[getIndex(b[j])] = b[j - 1] - '0';
}
else
{
vb[getIndex(b[j])] = 1;
}
}
for (int j = vc.length - 1; j >= 0; j--)
{
vc[j] = vc[j] + va[j] + vb[j];
if (vc[j] >= 10)
{
vc[j] = vc[j] - 10; if (j - 1 >= 0)
{
vc[j - 1] = vc[j - 1] + 1;
}
}
}
print(vc);
}
}
}
static void print(int[] c)
{
for (int i = 0; i < c.length; i++)
{
if (c[i] == 0)
{
continue;
}
if (c[i] == 1)
{
System.out.print(""+getChar(i));
}
else
{
System.out.print(c[i]+(getChar(i)+""));
}
}
System.out.println();
}
static int getIndex(char c)
{
switch (c)
{
case 'm': return 0;
case 'c': return 1;
case 'x': return 2;
case 'i': return 3;
default: return -1;
}
}
static char getChar(int i)
{
switch (i)
{
case 0: return 'm';
case 1: return 'c';
case 2: return 'x';
case 3: return 'i';
default: return '0';
}
}
}
C++ AC代码
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
const int INF = 0x3f3f3f3f;
int main()
{
#ifdef LOCAL
freopen("C:/input.txt", "r", stdin);
#endif
int mp[256];
mp['m'] = 1000, mp['c'] = 100, mp['x'] = 10, mp['i'] = 1;
string a, b;
int t;
cin >> t;
for (int ti = 0; ti < t; ti++)
{
cin >> a >> b;
int x = 0, y = 0, z = 1;
for (int i = 0; i < a.size(); i++)
if (isdigit(a[i]))
z = a[i] - '0';
else
x += mp[a[i]] * z, z = 1;
z = 1;
for (int i = 0; i < b.size(); i++)
if (isdigit(b[i]))
z = b[i] - '0';
else
x += mp[b[i]] * z, z = 1;
x += y;
if (x / 1000)
if (x / 1000 > 1)
cout << x / 1000 << "m";
else
cout << "m";
if (x / 100 % 10)
if ((x / 100 % 10) > 1)
cout << (x / 100 % 10) << "c";
else
cout << "c";
if (x / 10 % 10)
if ((x / 10 % 10) > 1)
cout << (x / 10 % 10) << "x";
else
cout << "x";
if (x % 10)
if ((x % 10) > 1)
cout << (x % 10) << "i";
else
cout << "i";
cout << endl;
}
return 0;
}