PTA甲级 1100 Mars Numbers (C++)

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 ) N (<100) N(<100). Then N N 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

Caution:

这道题简单来说就是双向翻译+进制转换,不过对我来说更有价值的一点是这道题涉及到了 string 类型一整行输入的方法(因为一行里面可能会有空格,直接cin >> s不能接收完整输入):函数 getline(),之前看到过 C 语言里面输入一整行的方法,但是因为 C 里面没有string,所以那些方法也不能直接用于string,查过之后发现了 getline 这个函数(注意:不是指 cin 的那个成员函数)用于输入一整行string
但是使用的时候还需要注意一点:如果直接把代码里面cin >> s;这一行改成getline(cin, s);的话,程序会出错,初步考虑是因为此时缓冲区的换行符还没有输进去,直接调用 getline 会只把换行符输进去,后面的s[0]就会越界,所以要把之前的scanf("%d", &n) 改成scanf("%d\n", &n);

Solution:

// Talk is cheap, show me the code
// Created by Misdirection 2021-08-12 17:01:52
// All rights reserved.

#include <iostream>
#include <vector>
#include <cmath>
#include <string>

using namespace std;

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

int main(){
    int n;
    scanf("%d\n", &n);

    for(int i = 0; i < n; ++i){
        string s;
        getline(cin, s);
        // cout << s.length() << endl;

        if(isdigit(s[0])){
            int len = s.length(), num = 0;

            for(int i = 0; i < len; ++i) num += (s[i] - '0') * pow(10, len - i - 1);

            if(num <= 12){
                cout << lessThan12[num] << endl;
                continue;
            }
            if(num % 13 == 0){
                cout << biggerThan12[num / 13 - 1] << endl;
                continue;
            }

            string num13 = "";
            while(num != 0){
                int yushu = num % 13;
                int shang = num / 13;

                num13 = (char)(yushu + '0') + num13;

                num /= 13;
            }    

            string mar = biggerThan12[num13[0] - '0' - 1] + ' ';
            mar = mar + lessThan12[num13[1] - '0'];

            cout << mar << endl;    
        }

        else{
            bool oneNum = false;

            for(int i = 0; i < 13; ++i){
                if(lessThan12[i] == s){
                    printf("%d\n", i);
                    oneNum = true;
                    break;
                }
            }

            for(int i = 0; i < 12; ++i){
                if(biggerThan12[i] == s){
                    printf("%d\n", (i + 1) * 13);
                    oneNum = true;
                    break;
                }
            }

            if(oneNum) continue;

            string num13 = "";

            for(int i = 0; i < 12; ++i){
                if(biggerThan12[i] == s.substr(0, 3)) num13 = (char)(i + '1');
            }

            for(int i = 0; i < 13; ++i){
                if(lessThan12[i] == s.substr(4, 3)) num13 += (char)(i + '0');
            }

            int num = 0;
            for(int i = 0; i < 2; ++i) num += (num13[i] - '0') * pow(13, 1 - i);
            printf("%d\n", num);
        }
        
    }

    return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

负反馈循环

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值