PAT1100. Mars Numbers (20)
2015-09-13 13:07
169 查看
本题是PAT2015.9.12-level A的第一题。我参加了该考试,但在此题答题过程中并没有通过所有的测试用例。回到家后重新整理了下。
题目信息如下:
People on Mars count their numbers with base 13:
Zero on Earth is called "tret" on Mars.
The numbers 1 to 12 on Earch is called "jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec" on Mars, respectively.
For the next higher digit, Mars people name the 12 numbers as "tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou", respectively.
For examples, the number 29 on Earth is called "hel mar" on Mars; and "elo nov" on Mars corresponds to 115 on Earth. In order to help communication between people from these two planets, you are supposed to write a program for mutual translation between
Earth and Mars number systems.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (< 100). Then N lines follow, each contains a number in [0, 169), given either in the form of an Earth number, or that of Mars.
Output Specification:
For each number, print in a line the corresponding number in the other language.
详见http://www.patest.cn/contests/pat-a-practise/1100
简单分析而言,此题是几个小问题的糅合:进制转换,字符串与数字转换。字符串处理。
数据结构方面,因为题目中给定了输入数据范围在100以内,故偷懒直接使用了100的定长数组。3个维度分别用于表示两个13进制位和输出语言标志位。
算法方面,使用stl的查找算法。
本题的特殊测试点在于,mars上的0是一个特殊的存在,整13倍数的数(13,26,39...)在表达时是不能出现0的。双向翻译时都需要注意这一点。
代码如下:
题目信息如下:
People on Mars count their numbers with base 13:
Zero on Earth is called "tret" on Mars.
The numbers 1 to 12 on Earch is called "jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec" on Mars, respectively.
For the next higher digit, Mars people name the 12 numbers as "tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou", respectively.
For examples, the number 29 on Earth is called "hel mar" on Mars; and "elo nov" on Mars corresponds to 115 on Earth. In order to help communication between people from these two planets, you are supposed to write a program for mutual translation between
Earth and Mars number systems.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (< 100). Then N lines follow, each contains a number in [0, 169), given either in the form of an Earth number, or that of Mars.
Output Specification:
For each number, print in a line the corresponding number in the other language.
详见http://www.patest.cn/contests/pat-a-practise/1100
简单分析而言,此题是几个小问题的糅合:进制转换,字符串与数字转换。字符串处理。
数据结构方面,因为题目中给定了输入数据范围在100以内,故偷懒直接使用了100的定长数组。3个维度分别用于表示两个13进制位和输出语言标志位。
算法方面,使用stl的查找算法。
本题的特殊测试点在于,mars上的0是一个特殊的存在,整13倍数的数(13,26,39...)在表达时是不能出现0的。双向翻译时都需要注意这一点。
代码如下:
#include<cstdio> #include<iostream> #include<vector> #include<list> #include<map> #include<string> #include<algorithm> using namespace std; int output[100][3]; string ten[13]={"", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"}; string one[13]={"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"}; #define debug int getValue(string input) { vector<string> tens(ten, ten+13), ones(one, one+13); vector<string>::iterator iter = std::find(tens.begin(), tens.end(), input); if(iter == tens.end()){ iter = std::find(ones.begin(), ones.end(), input); return (iter - ones.begin()); } else{ return 13*(iter - tens.begin()); } } int main(void) { #ifdef debug freopen("input.txt", "r", stdin); #endif int count; string temp; cin>>count; getchar(); for(int i=0;i<count;i++) { std::getline(cin, temp); if(temp[0]>'9' || temp[0]<'0'){ //char string::size_type pos = temp.find(' '); if(string::npos == pos){ //only 1 char int temp_value = getValue(temp); if(temp_value > 12){ output[i][0] = temp_value / 13; output[i][1] = 0; } else{ output[i][0] = 0; output[i][1] = temp_value; } } else{ //2 char string t1(temp.begin(), temp.begin()+pos), t2(temp.begin()+pos+1, temp.end()); output[i][0] = getValue(t1)/13; output[i][1] = getValue(t2); } output[i][2] = 0; //output in ineger } else{ int temp_value = std::atoi(temp.c_str()); //integer output[i][0] = temp_value / 13; output[i][1] = temp_value % 13; output[i][2] = 1; //output in chars. } } //output for(int i=0;i<count;i++){ if(output[i][2] == 0){ //output as integer cout<<output[i][0]*13 + output[i][1]<<endl; } else{ //output as chars. if(output[i][0] != 0 && output[i][1]==0){ /////////当整13的倍数时,0不予显示。 cout<<ten[output[i][0]]<<endl; } else if(output[i][0] != 0 && output[i][1]!=0){ cout<<ten[output[i][0]]<<' '<<one[output[i][1]]<<endl; } else{ cout<<one[output[i][1]]<<endl; } } } return 0; }
相关文章推荐
- 5、Android网络编程
- bash shell数组
- C++_std::bind std::function 高级用法
- 《javascript高级编程》读书笔记(两)javascript基本概念
- 网易云音乐歌词下载器
- DEDE列表调用有缩图显示缩图,无缩图则不显示缩图的方法
- EditText不自动获得输入焦点(以及 获得焦点但不弹出输入法软键盘的方法)
- ConcurrentHashMap之实现细节
- 任意匹配
- oracle ocfs2 安装配置
- LintCode "A + B Problem"
- 虚拟机安装 centos7 注意事项
- UC/OS II事件管理之信号量管理实例
- 19 个 Android 开发工具
- 使用ffmpeg库编译错误及解决办法
- windows 故障转移群集
- AD域控的备份及还原
- Zend Framework2-验证上传文件大小和扩展名,直接在调用函数验证的方法
- Effective C++ 条款55 让自己熟悉Boost
- 推断是否在多边形内的随机点2途径