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 11513
这个题有一些需要注意的细节,因为从第三个样例可以知道,在末尾没有0的情况有:次高位没有0,而且当只有0的时候,并没有进行进制的转换,直接结束,没有进栈,如果不注意,会导致答案错误。
代码:
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<sstream> #include<map> #include<string> #include<stack> using namespace std; int main(){ int n; string str; scanf("%d",&n); getline(cin,str); map<int,string>m_h; map<int,string>m_l; map<string,int>m_h2; map<string,int>m_l2; m_h[0] = m_l[0] = "tret"; m_h[1] = "tam";m_h[2] = "hel";m_h[3] = "maa";m_h[4] = "huh"; m_h[5] = "tou";m_h[6] = "kes";m_h[7] = "hei";m_h[8] = "elo"; m_h[9] = "syy";m_h[10] = "lok";m_h[11] = "mer";m_h[12] = "jou"; m_l[1] = "jan";m_l[2] = "feb";m_l[3] = "mar";m_l[4] = "apr"; m_l[5] = "may";m_l[6] = "jun";m_l[7] = "jly";m_l[8] = "aug"; m_l[9] = "sep";m_l[10] = "oct";m_l[11] = "nov";m_l[12] = "dec"; for(int i = 0;i<=12;i++){ m_h2[m_h[i]] = i; m_l2[m_l[i]] = i; } while(n--){ getline(cin,str); //cout<<str<<endl; int len = str.size(); string ss = str.substr(0,1); if(ss >= "0" && ss <= "9"){ stack<int>sta; //cout<<"yes"<<endl; int ans; bool last_second = false,use = false; stringstream ss; ss << str; ss >> ans; int k = 0; if(ans == 0)printf("tret"); //<<ans<<endl; while(ans){ sta.push(ans %13); ans /=13; k++; } while(!sta.empty()){ if(k > 1){ use = true; if(k == 2 && (sta.top()==0))last_second = true; cout<<m_h[sta.top()]; if(k>2)cout<<" "; } if(k == 1&&(last_second||sta.top())){ if(use)cout<<" "<<m_l[sta.top()]; else cout<<m_l[sta.top()]; } sta.pop(); k--; } puts(""); }else{ int flag = false; stack<string>sta_s; string sb; // reverse(str.begin(),str.end()); do{ int coun = str.find(" "); if(coun == -1){ sb = str.substr(0,len+1); //cout<<sb<<endl; sta_s.push(sb); break; } sb = str.substr(0,coun); str.erase(0,coun+1); sta_s.push(sb); // cout<<sb<<endl; }while(true); int base; int ans = 0; if(m_l2[sta_s.top()]){//if that have the lower digit,then to use the lower meaning base = 1; base *= 13; ans += m_l2[sta_s.top()]; sta_s.pop(); }else base = 13;//else the use is the higher digit; while(!sta_s.empty()){//to use the higher digit to translate the characer to number; //just to find the first element if is the lower ans += base * m_h2[sta_s.top()]; sta_s.pop(); base *= 13; } cout<<ans<<endl; } } }