poj2685——Numeral System新进制系统
来源:互联网 发布:礼仪卡片制作软件 编辑:程序博客网 时间:2024/06/05 09:55
Numeral System
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2227 Accepted: 1713
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.
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.
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
10xi x9ii 9ic2x2i 4c8x8im2ci 4m7c9x8i9c9x9i ii 9m9c9x8im ii mm9i i9m8c7xi c2x8i
Sample Output
3xx6cx5m9c9x9im9m9c9x9imimimx9m9c9x9i
整体思路:
先把数转化为十进制进行加减得出相应结果;
再将结果转换为新进制输出;
<span style="font-size:18px;">/** Filename: code.cpp* Created: 2016-04-29* Author: yunlong Wang *[mail:17744454343@163.com]* Desciption: Desciption*/#include <cstdio>#include <cstdlib>#include <iostream>#include <stack>#include <queue>#include <algorithm>#include <cstring>#include <cmath>#include <vector>#include <bitset>#include <list>#include <sstream>#include <set>#include <functional>using namespace std;#define INT_MAX 1 << 30#define MAX 100typedef long long ll;int n;char a[10],b[10];int ans;int change(char c){ int anss; if(c == 'm')anss = 1000; else if(c == 'c')anss = 100; else if(c == 'x')anss = 10; else if(c == 'i')anss = 1; return anss;}int c_d(char d[100]){ int summ = 0; for (int i = 0; i < strlen(d); i += 1) { if ((d[i] >= 50) && (d[i] <= 57)) { int j = i+1; int a = int(d[i]-48); //cout << a << endl; int b = change(d[j]); //cout << b << endl; summ += a*b; //cout << summ << endl; i++; } else { summ += change(d[i]); } } return summ;}int main(){ scanf("%d",&n); while (n--) { scanf("%s",a); scanf("%s",b); ans = c_d(a)+c_d(b); //将a,b转换为十进制数 //以下为将十进制数转换输出 int j = 0,a1,k = 0; char d[100] = {0}; a1 = ans; while (a1) //求整数的位数 { a1 /= 10; j++; } int t1,t2,t,j1=j; for (int l = 0; l < j; l += 1) //求整数中每一位并进行转换 { t1 = ans%int(pow(10.0,j1)); t2 = pow(10.0,j1-1); t = t1/t2; if(t == 1) { if(j1 == 4) d[k] = 'm'; else if(j1 == 3) d[k] = 'c'; else if(j1 == 2) d[k] = 'x'; else if(j1 == 1) d[k] = 'i'; } else if(t!=0) //为0时相应mcxi字符不显示 { int k1 = k+1; if(j1 == 4) {d[k] = (t+48);d[k1] = 'm';} else if(j1 == 3){d[k] = (t+48);d[k1] = 'c';} else if(j1 == 2){d[k] = (t+48);d[k1] = 'x';} else if(j1 == 1){d[k] = (t+48);d[k1] = 'i';} k++; } if(t!=0)k++; //为0时不移位,否则逢d[k]遇0停止输出 j1--; } cout << d << endl; } return 0;}</span>
1 0
- poj2685——Numeral System新进制系统
- System Permissions—— android系统权限
- Android系统UI——System Bars
- Android系统定制——Download Android System 及加载system镜像文件
- WCE之“系统事件”——System/Events
- Knife Article Publish System——小刀文章发布系统
- 传智播客——系统相关类:Runtime,System
- Unix Time Sharing System——Unix分时系统翻译
- WINCE之“系统事件”——System/Events
- java——深度解析System系统类
- Leapmotion开发笔记2——System Architecture系统架构
- Java 系统工具类 —— System、Scanner
- System Design——系统设计过程(二)抽象设计
- System Design——系统设计过程(三)理解瓶颈
- Roman Numeral Converter
- Roman Numeral Converter
- Roman Numeral Converter
- Roman Numeral Converter
- Spring事务管理的5种配置方式
- java5 CountDownLatch线程同步工具类
- Android开发笔记(九十四)图片的基本加工
- 标准clearfix 代码
- 百度登录算法分析和实现(下 - 实现篇)
- poj2685——Numeral System新进制系统
- Android.mk详解
- 关于文件上传踩过的坑multipart/form-data
- [Sencha ExtJS] 异步 JavaScript: Promises
- 一个稍微复杂点的hive sql
- 引导页_视差显示
- button贴图,点击事件带参数和不带参数连接
- 五一的任务
- mac的apache配置