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

本文共 2231 字,大约阅读时间需要 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 select, from ,join ,on ,where groupby,having ,order by limit的执行顺序和书写顺序
    查看>>
    MySQL Server 5.5安装记录
    查看>>
    mysql server has gone away
    查看>>
    mysql slave 停了_slave 停止。求解决方法
    查看>>
    MySQL SQL 优化指南:主键、ORDER BY、GROUP BY 和 UPDATE 优化详解
    查看>>
    MYSQL sql语句针对数据记录时间范围查询的效率对比
    查看>>
    mysql sum 没返回,如果没有找到任何值,我如何在MySQL中获得SUM函数以返回'0'?
    查看>>
    mysql Timestamp时间隔了8小时
    查看>>
    Mysql tinyint(1)与tinyint(4)的区别
    查看>>
    mysql union orderby 无效
    查看>>
    mysql v$session_Oracle 进程查看v$session
    查看>>
    mysql where中如何判断不为空
    查看>>
    MySQL Workbench 使用手册:从入门到精通
    查看>>
    mysql workbench6.3.5_MySQL Workbench
    查看>>
    MySQL Workbench安装教程以及菜单汉化
    查看>>
    MySQL Xtrabackup 安装、备份、恢复
    查看>>
    mysql [Err] 1436 - Thread stack overrun: 129464 bytes used of a 286720 byte stack, and 160000 bytes
    查看>>
    MySQL _ MySQL常用操作
    查看>>
    MySQL – 导出数据成csv
    查看>>
    MySQL —— 在CentOS9下安装MySQL
    查看>>