博客
关于我
B-1044 火星数字 (20 分)
阅读量:738 次
发布时间:2019-03-21

本文共 2225 字,大约阅读时间需要 7 分钟。

编写一个程序实现地球和火星数字之间的互译

火星人是使用13进制的计数方式。地球人数字翻译成火星文时,0被称为“tret”。地球数字1到12分别对应“jan”到“dec”。当数字超过12时,火星人使用“tam”(13)、“hel”(26)等高位词汇进行表示。在本式中,我们需要编写一个程序对地球和火星数字进行互译。

首先,我们建立一个地球和火星词汇的映射关系。地球数字0对应“tret”,1到12分别对应“jan”到“dec”;而13、26等分别对应“tam”到“jou”。

代码伪写:

#include 
#include
#include
#include
using namespace std;string one[] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};string two[] = {"", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};map
mpOne;map
mpTwo;int main() { int N, x; string str; // 初始化映射表 for (int i = 0; i < 13; i++) { mpOne[one[i]] = i; mpTwo[two[i]] = i; } // 读取输入 cin >> N; cin.get(); for (int i = 0; i < N; i++) { getline(cin, str); // 去除首尾空格 str.erase(0, str.find_first_not_of(" ")); if (str.empty()) { cout << "0" << endl; continue; } // 判断输入类型 if (isdigit(str[0])) { x = stoi(str); if (x == 0) { cout << "tret" << endl; } else { // 检查是否为13的倍数 if (x % 13 == 0) { cout << two[x / 13] << endl; } else { int current = x % 13; cout << two[x / 13] << " "; if (current != 0 || (x % 13 == 0)) { // 处理末尾的“tret” cout << one[current]; } cout << endl; } } } else { // 处理火星文字符串 size_t spacePos = str.find(' '); int high = 0; string highStr = str.substr(0, spacePos == string::npos ? 0 : spacePos); string lowStr = str.substr(spacePos == string::npos ? 0 : spacePos + 1); // 处理高位单词 if (!highStr.empty()) { auto highIt = mpTwo.find(highStr); if (highIt != mpTwo.end()) { high = highIt->second; } } // 处理低位单词 if (!lowStr.empty()) { auto lowIt = mpOne.find(lowStr); if (lowIt != mpOne.end()) { int current = lowIt->second % 13; x = (lowIt->second / 13) * 13; if (lowIt->second % 13 > 0) { x += current; } else { x += 13; } } else { x = 0; // 检查是否存在“tret” if (lowStr == "tret") { if (high == 1) high -= 13; // 前面的高位减13 x = 0; } } } else { x = high; } // 输出 if (high == 13 && lowStr.empty()) { cout << "tam" << endl; } else { cout << x << endl; } } } return 0;}

程序实现的主要功能包括:

  • 初始化两个映射表mpOne和mpTwo,将地球和火星词汇对应起来。
  • 读取输入,逐行处理。
  • 判断输入类型:
    • 如果是地球数字,转换为13进制,注意处理末尾的“tret”。
    • 如果是火星文,拆分为高位和低位,分别查找对应的地球数字,组合成地球文。
  • 输出翻译后的数字。
  • 代码还考虑了许多边界情况,如不存在的词汇应返回0,并对输入进行空格处理,确保正确读取行内容。这样,可以实现地球和火星数字之间的互译。

    转载地址:http://hqyrz.baihongyu.com/

    你可能感兴趣的文章
    mysql_secure_installation初始化数据库报Access denied
    查看>>
    MySQL_西安11月销售昨日未上架的产品_20161212
    查看>>
    Mysql——深入浅出InnoDB底层原理
    查看>>
    MySQL“被动”性能优化汇总
    查看>>
    MySQL、HBase 和 Elasticsearch:特点与区别详解
    查看>>
    MySQL、Redis高频面试题汇总
    查看>>
    MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
    查看>>
    mysql一个字段为空时使用另一个字段排序
    查看>>
    MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
    查看>>
    MYSQL一直显示正在启动
    查看>>
    MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
    查看>>
    MySQL万字总结!超详细!
    查看>>
    Mysql下载以及安装(新手入门,超详细)
    查看>>
    MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
    查看>>
    MySQL不同字符集及排序规则详解:业务场景下的最佳选
    查看>>
    Mysql不同官方版本对比
    查看>>
    MySQL与Informix数据库中的同义表创建:深入解析与比较
    查看>>
    mysql与mem_细说 MySQL 之 MEM_ROOT
    查看>>
    MySQL与Oracle的数据迁移注意事项,另附转换工具链接
    查看>>
    mysql丢失更新问题
    查看>>