PAT-A 真题 – 1100 Mars Numbers

发布于 / PAT-甲级 / 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.

Sample Input:

4
29
5
elo nov
tam

Sample Output:

hel mar
may
115
13

题目大意:火星文。要求将给定的火星数字转化成地球数字,地球数字转换成火星数字。

需要注意的有:

    1,第一个0可以用tret表示,十位非0个位不能为tret。

    2,建议用getline读数据,否则遇到空格程序会认为读完了

    3,如果使用getline,注意吸收掉第一个回车符号

    4,使用map打表法可以大大的提高写代码的速度

#include <iostream>
#include <algorithm>
#include <map>
#include <string>
#include <cstdio>
using namespace std;

map<string, int> mp_stoi;
map<int, string> mp_itos;
string _1[]  = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string _10[] = {"", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};

void init(){
  for(int i = 0; i <= 12; i++){
    for(int j = 0; j <= 12; j++){
      string MarsNum;
      if(i != 0){
        if(j == 0) MarsNum = _10[i];
        else MarsNum = _10[i] + " " + _1[j];
      }
      else MarsNum = _1[j];
      int EarthNum = i * 13 + j;
      mp_stoi[MarsNum] = EarthNum;
      mp_itos[EarthNum]= MarsNum;
      //printf("%d\t=>%s\n", EarthNum, MarsNum.c_str());
    } 
  }
}

int main(){
  init();
  int cnt;
  cin >> cnt;
  string tmp;
  getline(cin, tmp);  //吸收回车 
  for(int i = 0; i < cnt; i++){
    getline(cin, tmp);
    if(tmp[0] <= '9' && tmp[0] >= '0') cout << mp_itos[stoi(tmp)] << endl;
    else cout << mp_stoi[tmp] << endl;
  }
  return 0;
}

转载原创文章请注明,转载自: 斐斐のBlog » PAT-A 真题 – 1100 Mars Numbers
目前还没有评论,快来抢沙发吧~