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个字符串之间需要一个吸收字 ...
随机推荐
- Java进阶06 容器
Java中有一些对象被称为容器(container).容器中可以包含多个对象,每个对象称为容器中的一个元素.容器是用对象封装的数据结构(data structure). 充满梦想的容器 不同的数据结构 ...
- 【leetcode刷题笔记】Reverse Linked List II
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- OSS阿里云文件上传 demo。
所需jar包: aliyun-openservices-1.2.3.jar jdom-1.1.jar commons-codec-1.4.jar commons-logging-1.1.1.jar g ...
- 搭建DNS服务器-bind
1. 安装 yum install -y bind-chroot yum install -y bind-utils service named-chroot start 2. 修改配置 增加一 ...
- 2006浙大火星A+B
题目描述: 读入两个不超过25位的火星正整数A和B,计算A+B.需要注意的是:在火星上,整数不是单一进制的,第n位的进制就是第n个素数.例如:地球上的10进制数2,在火星上记为“1,0”,因为 ...
- Velocity的layout功能
一.从VelocityViewServlet到VelocityLayoutServlet 使用Velocity开发web应用时,需要在web.xml中配置一个Velocity提供的VelocityVi ...
- 【jQuery】jquery.metadata.js验证失效
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- Delegate Action<T in> Func<T in,out Tresult> Predicate<T>
action<T> 和 func<T> 都是delegate的简写形式,其中T为可以接受的参数类型 action<T> 指那些只有输入参数,没有返回值 Deleg ...
- .net关于应用程序缓存的一些疑惑
疑惑:获取缓存后强制转换为实体对象传递给前台,如果前台对这个实体对象中属性更改的话缓存中的数据也随之改变,为啥??? 首先是创建缓存的方法: /// <summary> /// 创建缓存项 ...
- [matlab]机器学习及SVM工具箱学习笔记
机器学习与神经网络的关系: 机器学习是目的,神经网络是算法.神经网络是实现机器学习的一种方法,平行于SVM. 常用的两种工具:svm tool.libsvm SVM分为SVC和SVR,svc是专门用来 ...