PAT A1100 Mars Numbers
Sample Input:
4
29
5
elo nov
tam
Sample Output:
hel mar
may
115
13
-
思路 1:
分别用 :char数组存储:数字->火星文(str),map存储:火星文(str)->数字
输入时先判断是数字还是字母
1)对于mars->e: 有两种情况(通过字符串长度判断)- 一位火星文 :直接通过map输出对应的数字
- 两位火星文:输出各位火星文对应数字的和(存入map时已将高位火星文*13)
2)对于e->mars: 也有两种情况 - 比12小(包含12):只输出一位火星文
- 比12大:如果刚好能整除13(num%13 == 0)只输出高位火星文,否则先输出高位火星文(num/13),再输出低位火星文(num%13)
-
code 1:
#include <iostream>
#include <algorithm>
#include <string>
#include <unordered_map>
#include <ctype.h>
using namespace std;
unordered_map<string, int> mars_e;
char fdig[15][5] = {"tret", "jan", "feb", "mar", "apr", "may",
"jun", "jly", "aug", "sep", "oct", "nov", "dec"};
char ndig[15][5] = {"tret", "tam", "hel", "maa", "huh", "tou",
"kes", "hei", "elo", "syy", "lok", "mer", "jou"};
int main(){
int n;
scanf("%d", &n);
getchar();
for(int i = 0; i < 13; ++i){
mars_e[fdig[i]] = i;
mars_e[ndig[i]] = i*13;
}
for(int i = 0; i < n; ++i){
string s;
getline(cin, s);
if(isalpha(s[0])){
//mars->e
if(s.size() == 3)
cout << mars_e[s]<<endl;
else{
string s1 = s.substr(0, 3);
string s2 = s.substr(4, 3);
cout << mars_e[s1]+mars_e[s2]<<endl;
}
}else{
//e-mars
int ans = stoi(s);
if(ans > 12){
cout << ndig[ans/13];
if(ans % 13 != 0)
cout << " " << fdig[ans%13]<<endl;
else
cout << endl;
}else{
cout << fdig[ans]<<endl;
}
}
}
return 0;
}
- T2 code:
#include <bits/stdc++.h>
using namespace std;
string d[2][13] = {
{"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"},
{"tret", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"}};
unordered_map<string, int> mp;
void Earth_Mars(int x){
if(x % 13 == 0){
printf("%s\n", d[1][x / 13].c_str()); //Wrong 2、4
}else if(x > 12){
printf("%s %s\n", d[1][x / 13].c_str(), d[0][x % 13].c_str());
}else{
printf("%s\n", d[0][x].c_str());
}
}
void Mars_Earth(string s){
if(s.size() == 3) printf("%d\n", mp[s]);
else{
string sub1 = s.substr(0, 3), sub2 = s.substr(4, 3);
int x = mp[sub1] + mp[sub2];
printf("%d\n", x);
}
}
int main(){
int n;
scanf("%d", &n);
getchar();
for(int i = 0; i < 13; ++i){
mp[d[0][i]] = i;
mp[d[1][i]] = 13 * i;
}
for(int i = 0; i < n; ++i){
string s;
getline(cin, s);
if(isalpha(s[0])){
Mars_Earth(s);
}else{
Earth_Mars(stoi(s));
}
}
return 0;
}
- T4 code:
#include <bits/stdc++.h>
using namespace std;
string low_dig[15] = {
"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"
};
string high_dig[15] = {
"", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"
};
unordered_map<string, int> mp;
int main()
{
//获得low_dig 和 high_dig 手改太tm累了
// string tmp;
// while(cin >> tmp)
// {
// tmp.erase(tmp.find(","));
// cout << tmp << "\", \"";
// }
for(int i = 0; i < 13; ++i)
{
mp[low_dig[i]] = i;
}
for(int i = 1; i < 13; ++i)
{
mp[high_dig[i]] = 13 * i;
}
int n;
scanf("%d", &n);
getchar(); //!!!!!!!!
while(n--)
{
string tmp;
getline(cin, tmp);
if(isalpha(tmp[0]))
{
string sub_high = tmp.substr(0, 3), sub_low = low_dig[0];
if(tmp.size() > 3)
{
sub_low = tmp.substr(4);
}
printf("%d", mp[sub_high] + mp[sub_low]);
}else
{
int x = stoi(tmp);
if(x > 12)
{
cout << high_dig[x / 13];
if(x % 13 != 0) printf(" ");
}
if(x == 0 || x % 13 != 0) //样例1: x = 0
{
cout << low_dig[x % 13];
}
}
printf("\n");
}
// system("pause"); //防止codeblock输入错误时闪退
return 0;
}