本文同步发布在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. PS 滤镜——(扭曲)球面化 Spherize

    %%%% Spherize clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorit ...

  2. cocos2dx & cocostudio 实现模态对话框

    用cocos2dx实现模态对话框 http://www.cnblogs.com/mrblue/(转自于) ui部分使用了cocoStudio,注意这里没有实现怎么屏蔽其他的输入事件,其他的文档已经太多 ...

  3. [原]NYOJ-6174问题-57

    大学生程序代写 /*6174问题 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 假设你有一个各位数字互不相同的四位数,把所有的数字从大到小排序后得到a,从小到大后得到b ...

  4. 分享几个高效编写JS 的心得

    原则 不要做任何优化除非的确需要优化   任何的性能优化都必须以测量数据为基础,如果你怀疑代码存在性能问题,首先通过测试来验证你的想法.性能优化三问 我还能做哪些工作从而让代码变得更有效率? 流行的J ...

  5. Skype SILK vs. iLBC vs. Speex

    对比一下这三种VOIP语音算法的特点: 1 参数与特征 2 SILK性能 关于iLBC和Speex的性能可以参考以前写的文章. 3 关于VOIP一些观点(仅代表个人观点) 1)  Skype 辛苦三年 ...

  6. Gym - 100570C: Subrect Query (巧妙的单调队列)

    De Prezer loves rectangles.He has a n × m rectangle which there is a number in each of its cells. We ...

  7. elasticsearch监控平台cerebro-0.8.3 相关操作

    上面这个平台是cerebro-0.8.3  在github上找就有了 #################### GET /hnscan_source_o_comm_drv_bad_bhv_occur/ ...

  8. ACM学习历程—CSU 1216 异或最大值(xor && 贪心 && 字典树)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1216 题目大意是给了n个数,然后取出两个数,使得xor值最大. 首先暴力枚举是C(n,  ...

  9. 让网站变灰的CSS代码(支持IE、FIREFOX和CHROME)(转)

    方法1:支持IE <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xht ...

  10. bzoj 3994 约数个数和 —— 反演+数论分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3994 推导过程和这里一样:https://www.cnblogs.com/MashiroSk ...