本文同步发布在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 - ) * ;
}

最新文章

  1. 开放封闭原则(Open Closed Principle)
  2. Ajax 学习 - 基础学习
  3. 【C语言学习趣事】_32_平胸的尴尬,嫁不出去的姑娘
  4. jsp页面中创建方法
  5. (转)Tarjan应用:求割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)
  6. Sikulix IDE简介
  7. 使用ajax与服务器通信的步骤
  8. JS~对img标签进行优化,使用onerror进行默认图像显示
  9. Ajax调用asp.net后台代码
  10. [Network]Application Layer
  11. Golang 微信机器人包
  12. C++中函数重载和函数覆盖的区别
  13. ES6优缺点
  14. angular 2 - 006 change detection 脏治检查 - DC
  15. Python--多线程处理
  16. ADO.NET 体系结构
  17. 报表系统OLAP
  18. Java WebService 知识点汇总
  19. 菜鸟vimer成长记——第2.0章、模式初探
  20. dfs序+主席树 或者 树链剖分+主席树(没写) 或者 线段树套线段树 或者 线段树套splay 或者 线段树套树状数组 bzoj 4448

热门文章

  1. informix 把数据从一个表倒到另外一个表中
  2. UnityShader实例15:屏幕特效之Bloom
  3. 汇编题目:在屏幕中间显示a-z的所有字母,按ESC键改变字符颜色
  4. 使用Visual Studio进行单元测试-Shim类中无法找到参数包含CancellationTokenSource的方法
  5. git常见使用情境整理
  6. 在Global Azure上用Azure CLI创建ARM的VM和面向公网的负载均衡
  7. 点击Button调用另一个Dialog
  8. 【转】 Pro Android学习笔记(五四):调试和分析(2):View层次结构
  9. java web基础学习 Forward和Redirect区别
  10. ubuntu dd烧录镜像文件