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

阅读: 评论:0

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

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

写在前面
  • 实现思路
    • 火星文最多2位,每行给出一个 [0, 169) 区间内的数字 —— 或者是地球文,或者是火星文
    • 火星文转数字,map<string,int>
    • 数字转火星文,字符串数组
    • 注意:
      • 13的倍数,火星文只有高位
      • 字符串读入方式,换行符处理
  • 题目较简单,细节处理耗费时间
测试用例
input:
4
29
5
elo nov
tam
output:
hel mar
may
115
13input:
1
13
output:
tam
ac代码
  • 由学习代码改造
#include <map>
#include <cstring>
#include <iostream>
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("%dn", &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("%sn", arrl[num].data());} else if(num>=13 && (num%13==0)) {printf("%sn",arru[num/13].data());} else {printf("%s %sn", arru[num/13].data(), arrl[num%13].data());}}elseprintf("%dn", 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 <iostream>
#include <string>
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));elsefunc2();cout << endl;}return 0;
}
知识点小结
// map初始化
#include <map>
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>
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!&#d(); it++) {cout << (*it).first << " " << (*it).second << endl;
}

本文发布于:2024-01-31 18:23:58,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170669663830471.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:火星   测试   注释   逻辑   数字
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23