本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90678474

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 Earth 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 (<). 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

题目大意:火星人用13进制计数,把地球的十进制与火星的13进制相互转换,需要注意的是火星人的数字是字符形式。

思路:映射火星人的数字与字符,用string存储读取的一整行数据,要调用getline()函数,它会读取一行里包括空格在内的所有字符。注意一下火星人的数字超过13时,0~12映射的字符也会变化,需要加入判断~

 #include <iostream>
#include <string>
#include <map>
using namespace std;
string Earth_Mars[] = { "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" };
map <string, int> Mars_Earth;
void init();
void earthToMars(string &s);
void marsToEarth(string &s);
int main()
{
init();
int N;
scanf("%d", &N);
getchar();
for (int i = ; i < N; i++) {
string s;
getline(cin, s);
if (s[] >= '' && s[] <= '')
earthToMars(s);
else
marsToEarth(s);
}
return ;
}
void marsToEarth(string &s) {
if (s == "tret") {
cout << Mars_Earth[s] << endl;
return;
}
int size = s.size();
if (size < )
cout << Mars_Earth[s] << endl;
else {
string s1 = s.substr(, ),
s2 = s.substr(, );
cout << Mars_Earth[s1] + Mars_Earth[s2] << endl;
}
}
void earthToMars(string &s) {
int earth = ;
for (int i = ; i < s.length(); i++)
earth = earth * + s[i] - '';
if (earth <= ) {
cout << Earth_Mars[earth] << endl;
return;
}
else if (earth % == ) {
cout << Earth_Mars[earth / + ] << endl;
return;
}
string mars[];
mars[] = Earth_Mars[earth % ];
earth = earth / ;
mars[] = Earth_Mars[earth + ];
cout << mars[] << " " << mars[] << endl;
}
void init() {
for (int i = ; i < ; i++)
Mars_Earth[Earth_Mars[i]] = i;
for (int i = ; i < ; i++)
Mars_Earth[Earth_Mars[i]] = (i - ) * ;
}

PAT甲级——1100 Mars Numbers (字符串操作、进制转换)的更多相关文章

  1. C语言拼接字符串以及进制转换

    #include<stdio.h> #include<stdlib.h> #include<string.h> char *join1(char *, char*) ...

  2. PAT A1010 Radix (25 分)——进制转换,二分法

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...

  3. PAT甲级——A1100 Mars Numbers

    People on Mars count their numbers with base 13: Zero on Earth is called "tret" on Mars. T ...

  4. 总结day3 ---- 进制转换,字符串切片,字符串常用方法.,for 循环,

    前情提要: int 的相关操作 进制转换 bit_lenth() str 的索引,以及常用的相关方法 for 循环 索引 切片 相关方法 一  : int 的相关操作 int 主要用于生活中的计算问题 ...

  5. 1100 Mars Numbers——PAT甲级真题

    1100 Mars Numbers People on Mars count their numbers with base 13: Zero on Earth is called "tre ...

  6. PAT 1100 Mars Numbers[难]

    1100 Mars Numbers (20 分) People on Mars count their numbers with base 13: Zero on Earth is called &q ...

  7. pat 1100 Mars Numbers(20 分)

    1100 Mars Numbers(20 分) People on Mars count their numbers with base 13: Zero on Earth is called &qu ...

  8. PAT甲级 进制转换题_C++题解

    进制转换题 PAT (Advanced Level) Practice 进制转换题 目录 <算法笔记> 重点摘要 1015 Reversible Primes (20) 1019 Gene ...

  9. 1100 Mars Numbers

    题意:进制转换. 思路:注意当数字是13的倍数时,只需高位叫法的单词.比如26,是“hel”,而不是“hel tret”.我被坑在这里了!对应语句1的处理.另外,在输入n和n个字符串之间需要一个吸收字 ...

随机推荐

  1. ACM学习历程—HDU 3949 XOR(xor高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题目大意是给n个数,然后随便取几个数求xor和,求第k小的.(重复不计算) 首先想把所有xor的 ...

  2. Ubuntu 14.04开发环境

    安装ssh服务:sudo apt-get install openssh-server 安装vim:sudo apt-get install vim-gtk 安装gparted:sudo apt-ge ...

  3. iOS重写drawRect方法实现带箭头的View

    创建一个UIView的子类,重写drawRect方法可以实现不规则形状的View,这里提供一个带箭头View的实现代码: ArrowView.h #import <UIKit/UIKit.h&g ...

  4. Oracle 12C 新特性之级联truncate

    12c之前的版本中,在子表引用一个主表以及子表存在记录的情况下,是不提供截断此主表操作的.而在 12c 中的带有 CASCADE 操作的TRUNCATE TABLE 可以截断主表中的记录,并自动对子表 ...

  5. centos7添加环境变量

    # vim /etc/profile在最后,添加:export PATH="/usr/local/webserver/mysql/bin:$PATH" #添加的路径保存,退出,然后 ...

  6. asp.net分页asp.net无刷新分页高效率分页

    项目中经常会用到分页的功能类似的项目做过无数个了,今个把自己常用的分页代码分享一下. 首先说说服务端处理的代码: 下面代码中重点是分页的sql语句的写法,其中的参数@n是当前的页码,总的来说本服务端主 ...

  7. HDU1698(线段树入门题)

    Just a Hook Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descrip ...

  8. java代码Math.sqrt

    总结:这个判断小数的题目,当时全只2有一个人想出了结果.老师很开心.我很桑心~~~~ 我没想到要取膜,我只想到了除以等于0就够了.至于中间的“取膜”,我没凑齐来,还是不够灵活 package com. ...

  9. Go语言命令行操作命令详细介绍

    转自:http://www.jb51.net/article/56781.htm Go 命令 Go语言自带有一套完整的命令操作工具,你可以通过在命令行中执行go来查看它们: 图 Go命令显示详细的信息 ...

  10. 安装pyenv版本管理

    系统:Centos7.4 安装pyenv是为了更好的管理python的版本. 在进行安装操作之前,首先使用普通用户test,进行操作,如下: #安装之前先安装依赖的库 [test@localhost ...