PAT甲级——1100 Mars Numbers (字符串操作、进制转换)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90678474
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 (字符串操作、进制转换)的更多相关文章
- C语言拼接字符串以及进制转换
#include<stdio.h> #include<stdlib.h> #include<string.h> char *join1(char *, char*) ...
- PAT A1010 Radix (25 分)——进制转换,二分法
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- PAT甲级——A1100 Mars Numbers
People on Mars count their numbers with base 13: Zero on Earth is called "tret" on Mars. T ...
- 总结day3 ---- 进制转换,字符串切片,字符串常用方法.,for 循环,
前情提要: int 的相关操作 进制转换 bit_lenth() str 的索引,以及常用的相关方法 for 循环 索引 切片 相关方法 一 : int 的相关操作 int 主要用于生活中的计算问题 ...
- 1100 Mars Numbers——PAT甲级真题
1100 Mars Numbers People on Mars count their numbers with base 13: Zero on Earth is called "tre ...
- PAT 1100 Mars Numbers[难]
1100 Mars Numbers (20 分) People on Mars count their numbers with base 13: Zero on Earth is called &q ...
- pat 1100 Mars Numbers(20 分)
1100 Mars Numbers(20 分) People on Mars count their numbers with base 13: Zero on Earth is called &qu ...
- PAT甲级 进制转换题_C++题解
进制转换题 PAT (Advanced Level) Practice 进制转换题 目录 <算法笔记> 重点摘要 1015 Reversible Primes (20) 1019 Gene ...
- 1100 Mars Numbers
题意:进制转换. 思路:注意当数字是13的倍数时,只需高位叫法的单词.比如26,是“hel”,而不是“hel tret”.我被坑在这里了!对应语句1的处理.另外,在输入n和n个字符串之间需要一个吸收字 ...
随机推荐
- xml字符串转xml对象,xml对象转json对象
xml字符串转xml对象: function loadXml(str) { if (str == null) { return null; } var doc = str; try{ doc = cr ...
- Agc017_E Jigsaw
传送门 题目大意 有$n$块拼图,每一块都由左中右三个部分组成,每块拼图中间部分是高为$H$的长方形,对于第$i$块品推左侧是高为$A_i$距离底部为$C_i$的长方体,右侧是高位$B_i$距底部为$ ...
- Wannafly #4 F 线路规划
数据范围252501 劲啊 Q国的监察院是一个神秘的组织. 这个组织掌握了整个Q国的地下力量,监察着Q国的每一个人. 监察院一共有N个成员,每一个成员都有且仅有1个直接上司,而他只听从其上直接司的命令 ...
- loj514模拟只会猜题意
果然是道模拟... 一开始想线段树 看了一眼数据范围:“这tm不是前缀和吗” 然后水过 #include<iostream> #include<cstdio> #include ...
- [Luogu3960][NOIP2017]列队
luogu sol 震惊!\(NOIP\)居然也出数据结构! 话说回来,其实只需要对每一行的前\(m-1\)个人维护一个数据结构,然后对最后一列的\(m\)个人也维护一个数据结构就好了.具体的话写平衡 ...
- JAVAset容器基本知识
import java.io.PrintWriter; import java.util.HashSet; import java.util.Scanner; import java.util.Ite ...
- keepalived+nginx实现双机热备
keepalived是一个类似于layer3, 4, 5 交换机制的软件,也就是我们平时说的第3层.第4层和第5层交换.Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机, ...
- CoreData的使用(IOS学习)
——杂言:最近开始学习IOS7的开发,下文是在已经建好的项目里加入CoreData的结构,并实现一个基于coredata的简单save,query. 1. 引入Core Data Framework. ...
- fsck磁盘检查修复
fsck 使用方式 : fsck [-sACVRP] [-t fstype] [--] [fsck-options] filesys [...]说明 : 检查与修复 Linux 档案系统,可以同时检查 ...
- HTML5新增的结构元素
HTML5的结构 一:新增的主体结构元素 在HTML5中,为了使文档的结构更加清晰明确,追加了几个与页眉,页脚内容区块等文档结构相关联的结构元素. 1.1article元素 article元素代表文档 ...