PAT1100:Mars Numbers
Posted 0kk470
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT1100:Mars Numbers相关的知识,希望对你有一定的参考价值。
1100. Mars Numbers (20)
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.
Sample Input:4 29 5 elo nov tamSample Output:
hel mar may 115 13
思路
进制转换和字符串处理。
1.分为地球数字转火星文、火星文转地球数字两种情况。
2.火星文转地球数字分为三种情况:1)单独一个火星数字 2)两个火星数字(第二个火星数字不是tret) 3)两个火星数字(第二个火星数字一定是tret(零))。
代码
#include<iostream> #include<vector> #include<string> using namespace std; vector<string> num = {"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"}; vector<string> highernum = {"sb","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok","mer", "jou"}; int search(string s) { for(int i = 0;i < num.size();i++) { if(s == num[i]) return i; } for(int i = 0;i < highernum.size();i++) { if(s == highernum[i]) return -i; } return 0; } int main() { int N; scanf("%d",&N); getchar(); while(N--) { string number; getline(cin,number); if(number[0] <= ‘9‘) { int n = stoi(number); int first = n/13,second = n % 13; if(n == 0) cout << num[n] <<endl; else { if(first !=0 && second != 0) cout << highernum[first]<< " " <<num[second] << endl; else if(first != 0) cout << highernum[first] << endl; else if(second != 0) cout << num[second] << endl; } } else if(number.size() == 3) { int res = search(number); if(res >= 0) cout << res <<endl; else cout << -res * 13 << endl; } else if(number.size() == 7) { string fst = number.substr(0,3); string sec = number.substr(4,3); cout << -search(fst) * 13 + search(sec) << endl; } else { string fst = number.substr(0,3); cout << -search(fst) * 13 << endl; } } }
以上是关于PAT1100:Mars Numbers的主要内容,如果未能解决你的问题,请参考以下文章
1100. Mars Numbers (20)字符串处理——PAT (Advanced Level) Practise
1100. Mars Numbers (20)字符串处理——PAT (Advanced Level) Practise