1044 火星数字(附详细注释,逻辑分析,测试点2、测试点4)

写在前面

  • 实现思路
    • 火星文最多2位,每行给出一个 [0, 169) 区间内的数字 —— 或者是地球文,或者是火星文
    • 火星文转数字,map
    • 数字转火星文,字符串数组
    • 注意:
      • 13的倍数,火星文只有高位
      • 字符串读入方式,换行符处理
  • 题目较简单,细节处理耗费时间

测试用例

input:
4
29
5
elo nov
tam
output:
hel mar
may
115
13

input:
1
13
output:
tam

ac代码

  • 由学习代码改造
#include 
#include 
#include 
using namespace std;
string arrl[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string arru[13] = {"####", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
map<string,int> mapl = {{"tret",0}, {"jan",1}, {"feb",2}, {"mar",3}, {"apr",4}, {"may",5}, {"jun",6}, {"jly",7}, {"aug",8}, {"sep",9}, {"oct",10}, {"nov",11}, {"dec",12}};
map<string,int> mapu = {{"tam",1}, {"hel",2}, {"maa",3}, {"huh",4}, {"tou",5}, {"kes",6}, {"hei", 7}, {"elo",8}, {"syy",9}, {"lok", 10}, {"mer", 11}, {"jou",12}};

int main()
{
    int n;
    scanf("%d\n", &n);
    string s;
    for (int i = 0; i < n; i++)
    {
        getline(cin, s);
        if (s[0] >= '0' && s[0] <= '9')
        {
            int num = stoi(s);
            if(num<13) {
                printf("%s\n", arrl[num].data());
            } else if(num>=13 && (num%13==0)) {
                printf("%s\n",arru[num/13].data());
            } else {
                printf("%s %s\n", arru[num/13].data(), arrl[num%13].data());
            }
        }
        else
            printf("%d\n", s.length()==3 ? mapl[s]+mapu[s]*13 : mapu[s.substr(0,3)]*13+mapl[s.substr(4,3)]);
    }
    return 0;
}

学习代码

  • 1044. 火星数字(20).cpp
#include 
#include 
using namespace std;
string a[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string b[13] = {"####", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
string s;
int len;
void func1(int t) {
    if (t / 13) cout << b[t / 13];
    if ((t / 13) && (t % 13)) cout << " ";
    if (t % 13 || t == 0) cout << a[t % 13];
}
void func2() {
    int t1 = 0, t2 = 0;
    string s1 = s.substr(0, 3), s2;
    if (len > 4) s2 = s.substr(4, 3);
    for (int j = 1; j <= 12; j++) {
        if (s1 == a[j] || s2 == a[j]) t2 = j;
        if (s1 == b[j]) t1 = j;
    }
    cout << t1 * 13 + t2;
}
int main() {
    int n;
    cin >> n;
    getchar();
    for (int i = 0; i < n; i++) {
        getline(cin, s);
        len = s.length();
        if (s[0] >= '0' && s[0] <= '9')
            func1(stoi(s));
        else
            func2();
        cout << endl;
    }
    return 0;
}

知识点小结

// map初始化
#include 
map<string,int> mapu = {{"tam",1}, {"hel",2}, {"maa",3}, {"huh",4}, {"tou",5}, {"kes",6}, {"hei", 7}, {"elo",8}, {"syy",9}, {"lok", 10}, {"mer", 11}, {"jou",12}};

// map迭代输出
#include 
map<string,int> mapu = {{"tam",1}, {"hel",2}, {"maa",3}, {"huh",4}, {"tou",5}, {"kes",6}, {"hei", 7}, {"elo",8}, {"syy",9}, {"lok", 10}, {"mer", 11}, {"jou",12}};

map<string, int>::iterator it;
for(it=mapu.begin(); it!=mapu.end(); it++) {
    cout << (*it).first << " " << (*it).second << endl;
}

你可能感兴趣的:(PAT(乙级),算法比赛相关)