1100 Mars Numbers (20 分)

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 (<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

code

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

string a[] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string b[] = {"", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};

int change(string word)
{
    for(int i = 0; i < 13; i++){//0~13
        if(a[i]==word) return i;
    }
    for(int i = 0; i < 13; i++){//0~13
        if(b[i]==word) return i*13;
    }
}

int main()
{
    int n;
    string str;
    scanf("%d",&n);
    getchar();//*
    while(n--){
        getline(cin,str);
        if(isdigit(str[0])){
            int d = atoi(str.c_str()),r;
            r = d % 13;
            d /= 13;
            if(d!=0){
                if(r!=0) cout << b[d] << " " << a[r] << endl;
                else cout << b[d] << endl;
            }
            else cout << a[r] << endl;
        }
        else {
            string word;
            int ans = 0;
            for(int i = 0; i < str.size(); i++){
                if(str[i]!=' '){
                    word += str[i];
                }
                else {
                    ans += change(word);
                    word = "";
                }
            }
            ans += change(word);
            printf("%d\n",ans);
        }
    }
    return 0;
}

解题思路

  1. 火星上是13进制(由12位数组成各位和12位数组成10位可推断,由数据范围169=13*13也可推断)
  2. 一开始会纠结于输入方式的选择,输入一行个既可能是数字又可能是字符串并且包含空格,因此输入之后还要进行判断与分割,想想似乎有点复杂,其实不然
  3. 选择getline函数和string类型输入一行可包含空格的字符串:
string str;
getline(cin,str);
  1. 因为输入只有两种格式:
  • 地球文(纯数字0~168)

  • 火星文(纯字母)

    因此判断每行输入的首字符是数字还是字母即可判断输入的是地球文还是火星文;用isdigit(char)判断即可

  1. 对于输入地球文(0~168),将数字转换为火星文即可,首先将输入的string类型转换为int类型,再进项转换(由于str是string类型,因此用atoi(str.c_str)
    • 数字整除进制13所得除数和余数分别为火星文所对应的十位数和个位数(即第一个字符串和第二个字符串)所在两个string数组中的位置,如29/13=2······3,因此输出b[2]和a[3]即hel mar
    • 注意十位数和个位数是否存在的格式问题
  2. 对于火星文,算是比较简单的字符串分割问题
    • 写一个change()函数将火星文对应的数字返回
    • 将每个火星文对应的数字相加即可
    • hel返回13*2=26,mar返回3,最后结果即为26+3

note(遇到的各种错误情况)

  1. 未考虑0
  2. change数组循环0~12,粗心大意
  3. 数字和字符之间输入时要插入getchar()
  4. 除数为0和余数为0的组合情况,与输出格式有关

:题目不难,但要短时间内做到思路清晰却不简单,而PAT最考察的反而是这一点,因而平时练习时还是需要在短时间内快速解题。如果难以做到的话,说明要注重平时的积累和反思。有一些知识点也是PAT中出现和考察的,这些要更加注意。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值