博客
关于我
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 数据类型和属性
    查看>>
    mysql 敲错命令 想取消怎么办?
    查看>>
    Mysql 整形列的字节与存储范围
    查看>>
    mysql 断电数据损坏,无法启动
    查看>>
    MySQL 日期时间类型的选择
    查看>>
    Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
    查看>>
    MySQL 是如何加锁的?
    查看>>
    MySQL 是怎样运行的 - InnoDB数据页结构
    查看>>
    mysql 更新子表_mysql 在update中实现子查询的方式
    查看>>
    MySQL 有什么优点?
    查看>>
    mysql 权限整理记录
    查看>>
    mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
    查看>>
    MYSQL 查看最大连接数和修改最大连接数
    查看>>
    MySQL 查看有哪些表
    查看>>
    mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
    查看>>
    MySql 查询以逗号分隔的字符串的方法(正则)
    查看>>
    MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
    查看>>
    mysql 查询数据库所有表的字段信息
    查看>>
    【Java基础】什么是面向对象?
    查看>>
    mysql 查询,正数降序排序,负数升序排序
    查看>>