1100. Mars Numbers (20)

题目链接:http://www.patest.cn/contests/pat-a-practise/1100
题目:

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

分析:
有很多的注意点,比如对于0和tret的处理,对于输入要是行的处理,以及对于26的话要输出hel后面不能带空格。代码中都有详细的注释
比如数字变成字符串的时候,要考虑十位是否有(小于12的情况),个位是否有(刚好被整除以及0的情况)还有十位个位都有的情况(中间需要加空格,其余的情况不能有空格)
案例2是输入是0,输出要是tret的例子。
AC代码:
#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<string>
using namespace std;
string mars[25] = { "tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec",
"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou" };
//分别存储0-12以及13的1到12倍
string earth2mars(int x){
 if (x / 13 == 0){
  return mars[x];
 }
 else if (x % 13 == 0){
  return mars[12 + x / 13];
 }
 else{
  return mars[12 + x / 13] + " " + mars[x % 13];
 }
}
int mars2earth(string x){
 int ge = 0, shi = 0;
 int idx = x.find(" ");//查找有没有空格
 if (idx == string::npos){//没有空格,只有一个数
  for (int i = 0; i < 25; ++i){
   if (mars[i] == x){
    if (i <= 12)ge = i;//是个位的情况
    else shi = i - 12;//是十位的情况
    break;
   }
  }
 }
 else{
  string shi_str = x.substr(0, idx);//提取出火星文十位的部分
  string ge_str = x.substr(idx + 1, x.size() - idx);//提取出火星文个位的部分
  for (int i = 0; i < 13; ++i){
   if (mars[i] == ge_str){
    ge = i;//是个位的情况
    break;
   }
  }
  for (int i = 13; i < 25; ++i){
   if (mars[i] == shi_str){
    shi = i - 12;//是个位的情况
    break;
   }
  }
 }
 return ge + shi * 13;
}
int main(){
 //freopen("F://Temp/input.txt", "r", stdin);
 int n;
 cin >> n;
 getchar();//获取最后的回车符,因为后面是getline
 string str;
 while (n--){
  getline(cin, str);//获取整行
  if (str[0] <= '9' && str[0] >= '0'){//是地球文
   cout << earth2mars(atoi(str.c_str())) << endl;
  }
  else{//是火星文
   cout << mars2earth(str) << endl;
  }
 }
 return 0;
}


截图:

——Apie陈小旭
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值