PAT 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

大坑题,如果不看别人的题解,我可能一辈子都满分不了

题意不介绍了,直接说坑点。

。。。。。

坑点是:对于是13整数倍的数字例如13、26、39…… 最后转换成字符串的时候不能加 tret(0)

比如按我们之前的理解:13 答案是 tam  tret  但是 在这道题中 正确答案 是 tam

……pat 坑真多啊。 无力吐槽

#include <bits/stdc++.h>
using namespace std;
//低位
string num[13]={
 "tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug","sep", "oct", "nov", "dec"
};
//高位
string num1[13]={
"tret","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"
};
//string数字转换成int类型
int change_int(string s){
    int ans = 0 , len = s.length();
    for(int i = 0; i < len; i++) {
        ans = ans * 10 + (s[i] - '0');
    }
    return ans;
}
//数字转换成string
string change1(int s){
    string temp = "";
    if(s < 13) {
        temp += num[s];
    }else {
        int A1,A2;
        A1 = s % 13; // 低位 
        s /= 13;
        A2 = s % 13; //高位
        if(A1 != 0) 
        temp = num1[A2] + " " + num[A1];
        else temp = num1[A2];
    }
    return temp;
}
int pos(string s) {
    
    for(int i = 0; i < 13; i++) {
        if(num[i] == s) return i;
    }
    return 0;
}
int pos1(string s){
    for(int i = 0; i < 13; i++) {
        if(num1[i] == s) return i; 
    }
    return 0;
}
//string 转换成 数字
int change2(string s){
    int flag = 0; //看是否有空格 ,并记录位置
    int len = s.length(), ans = 0; //长度 和答案
    for(int i = 0; i < len;i++) {
        if(s[i] == ' ') {
            flag = i; break;
        }
    }
    if(!flag) {
        ans = pos1(s)*13 + pos(s);
    }else {
        string temp = s.substr(0,flag);
        string temp1 = s.substr(flag+1);
        // cout << temp <<" " << temp1 << endl;
        
        ans = pos1(temp)*13 + pos(temp1);
    }
    return ans;
}
int main() 
{
    int t;
    string a;
    cin >> t;
    getchar();
    while(t--) {
        getline(cin, a);
        string ans;
        int ans1;
        if(a[0] >= '0' && a[0] <= '9'){
            int temp_a = change_int(a);
            ans = change1(temp_a);
            cout << ans << endl;
        }else {
            ans1 = change2(a);
            cout << ans1 << endl;
        }
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值