ZOJ-#3501 Roman Order(字符串模拟)
2014-09-19 16:58
225 查看
题目大意:给出一组数字对应的Roman值,再给出一组数据,将数据按照对应的Roman值的字典序排序输出。
解题思路:先将Roman值对应的值存下来,对输入的数据利用结构体将数值和对应的Roman字符捆绑在一起存储,然后定义cmp按照字典序排序即可。详见code。
题目来源:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4344
code:
解题思路:先将Roman值对应的值存下来,对输入的数据利用结构体将数值和对应的Roman字符捆绑在一起存储,然后定义cmp按照字典序排序即可。详见code。
题目来源:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4344
code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 1e4+10; int t,n; struct node{ int data; string str; }a[MAXN]; string base[4][9]={ {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}, {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}, {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}, {"M", "MM", "MMM"} }; int cmp(const node a,const node b){ int lena=a.str.length(); int lenb=b.str.length(); for(int i=0;i<min(lena,lenb);i++) if(a.str[i]!=b.str[i]) return a.str[i]<b.str[i]; return lena<lenb; } int main(){ //freopen("input.txt","r",stdin); scanf("%d",&t); while(t--){ scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&a[i].data); int k=0,tmp=a[i].data; string arr=""; while(tmp){ if(tmp%10>0) arr=base[k][tmp%10-1]+arr; k++; tmp/=10; } a[i].str=arr; } sort(a,a+n,cmp); for(int i=0;i<n-1;i++) printf("%d ",a[i].data); printf("%d\n",a[n-1].data); } return 0; }
相关文章推荐
- ZOJ Problem Set - 3490 String Successor(字符串模拟)
- ZOJ 3826 Hierarchical Notation(2014 牡丹江 H,字符串模拟)
- 字符串的处理(模拟) ZOJ Problem Set - 1099 HTML
- POJ 1782/ ZOJ 2240 : Run Length Encoding - 字符串处理(模拟)
- ZOJ 3826 Hierarchical Notation(模拟-字符串处理)
- zoj 1016 字符串模拟 ~小狼回来做题了!!
- ZOJ 3826 hash+字符串模拟
- ZOJ-#3465 The Hive(字符串模拟)
- zoj 1151 Word Reversal(字符串操作模拟)
- ZOJ 3501 Roman Order
- zoj-3826(字符串模拟)
- ZOJ 3985 String Of CCPC 字符串,模拟
- ZOJ_3501 Roman Order
- ZOJ 3930 Dice Notation【模拟】【字符串】
- ZOJ 3826 Hierarchical Notation(亚洲区域赛牡丹江站 字符串模拟)
- codeforces 600 A. Extract Numbers(字符串模拟)
- uva 156 Ananagrams(字符串模拟搜索)
- 模拟实现字符串查找函数strstr
- zoj 3627#模拟#枚举
- ACdream 1188 Read Phone Number(字符串:模拟)