1100. Mars Numbers

Mars Numbers

People on Mars count their numbers with base 13:

  • Zero on Earth is called “tret” on Mars.
  • The numbers 1 to 12 on Earch 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 (< 100). 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进制,用一个长度3的字符串表示一个地球数字,火星数字最多只有两位,且高位和低位相同数字的表示方式不同,要求将给定的地球(火星)数字转换成对应的火星(地球)数字并输出。

思路

整体思路上就是13进制转换和数字表示方式转换,没有什么问题。关键在细节处理上有很多坑,主要有以下几点:

  • 输入的火星数字只有一位时,要判断是高位数字还是低位数字,低位直接转换输出,高位则要乘以13再输出;
  • 地球数字转换成火星数字时,如果存在高位且低位为0,则不输出低位,只输出高位。

代码实现

#include <iostream>
#include <string>
#include <map>
using namespace std;

string to_M_Lowwer[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string to_M_Higher[13] = {"tret", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
map<string, int> to_E_Higher, to_E_Lowwer;

void createMap()        // 映射创建
{
    for (int i = 0; i < 13; i++)
    {
        to_E_Higher[to_M_Higher[i]] = i;
        to_E_Lowwer[to_M_Lowwer[i]] = i;
    }
}

void solve(string s)
{
    if (s[0] >= '0' && s[0] <= '9')             // 输入为地球数字
    {
        int num = 0;
        for (int i = 0; i < s.length(); i++)
            num = num * 10 + s[i] - '0';        // 求具体数字
        int b = num % 13;                       // 低位
        int a = num / 13;                       // 高位
        if (a != 0)                             // 存在高位
            if (b != 0)                         // 低位不为0,高低位都要输出
                cout << to_M_Higher[a] << " " << to_M_Lowwer[b] << endl;
            else                                // 低位为0,只输出高位
                cout << to_M_Higher[a] << endl;
        else                                    // 只存在低位
            cout << to_M_Lowwer[b] << endl;
    }
    else if (s.length() == 7)                   // 输入为火星数字,且高低位都存在
    {
        string x = s.substr(0, 3);              // 字符串拆分
        string y = s.substr(4, 3);
        int a = to_E_Higher[x];
        int b = to_E_Lowwer[y];
        cout << a * 13 + b << endl;
    }
    else if (to_E_Higher.find(s) != to_E_Higher.end())      // 只输入高位火星数字
        cout << to_E_Higher[s] * 13 << endl;
    else                            // 输入低位火星数字
        cout << to_E_Lowwer[s] << endl;
}

int main()
{
    int n;
    string s;

    createMap();

    cin >> n;
    getchar();
    for (int i = 0; i < n; i++)
    {
        getline(cin, s);
        solve(s);
    }

    return 0;
}
### MIPS 架构在 MARS 模拟器中的使用 MARS 是一个专门用于教育目的的 MIPS 汇编语言模拟器,它为学习和理解 MIPS 架构提供了良好的环境[^1]。MARS 的版本 3.2.1 在教程中被广泛使用,帮助用户探索 MIPS 汇编语言的核心功能及其在 CPU 设计中的应用。 MIPS 架构的一个显著特点是其固定的指令格式,所有指令均采用 32 位长度表示。尽管这种设计可能显得“死板”,并且在存储空间利用上不够高效,但它带来了规律性和简单性,降低了 CPU 设计的复杂度[^2]。因此,MIPS 架构非常适合初学者学习计算机体系结构和汇编语言的基础知识。 在 MARS 模拟器中,用户可以编写、调试和运行 MIPS 汇编代码。以下是 MARS 模拟器的一些关键功能和使用方法: #### 1. 界面布局与工具 MARS 提供了一个直观的图形用户界面(GUI),包括代码编辑器、寄存器视图、数据段视图以及控制台输出窗口。这些工具使得用户能够轻松地观察程序执行过程中的状态变化。 #### 2. 汇编代码的编写与调试 用户可以在 MARS 中编写 MIPS 汇编代码,并通过单步执行、断点设置等功能逐步分析程序行为。例如,以下是一个简单的 MIPS 汇编代码示例,用于计算两个数的和: ```assembly # Example of adding two numbers in MIPS assembly .data num1: .word 5 num2: .word 10 result: .word 0 .text .globl main main: # Load values from memory lw $t0, num1 lw $t1, num2 # Add the values add $t2, $t0, $t1 # Store the result back to memory sw $t2, result # Exit program li $v0, 10 syscall ``` #### 3. 寄存器与内存管理 MARS 允许用户实时查看和修改通用寄存器、特殊寄存器以及内存内容。这对于理解 MIPS 架构中的数据流动和存储机制非常有帮助。 #### 4. 教学优势 由于 MIPS 指令集的规则性和一致性,MARS 模拟器成为学习计算机体系结构的理想工具。它不仅帮助学生掌握汇编语言编程技巧,还让他们深入了解 CPU 的内部工作原理[^2]。 ### 总结 MARS 模拟器为 MIPS 架构的学习提供了强大的支持,其固定指令格式的特性虽然存在一些缺点,但极大地简化了教学和实践过程。通过 MARS,用户可以更深入地理解 MIPS 汇编语言及其在 CPU 设计中的应用。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值