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
#include <iostream>
using namespace std;
const string low[12] = {"jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
const string high[12] = {"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
bool isEarth(string number)
{
if (number[0] >= '0' && number[0] <= '9')
return true;
return false;
}
string to_Mars(string earth)
{
string res;
int number = stoi(earth);
if (!number)
return "tret";
if (number % 13)
res = low[number % 13 - 1] + res;
if (number / 13)
res = high[number / 13 - 1] + " " + res;
return res;
}
int to_Earth(string number)
{
int res = 0;
if (number.size() == 7)
{
string l;
string h;
for (int i = 0; i < 3; i++)
{
h = h + number[i];
l = l + number[i + 4];
}
for (int i = 0; i < 12; i++)
if (low[i] == l)
res += i + 1;
for (int i = 0; i < 12; i++)
if (high[i] == h)
res += (i + 1) * 13;
}
else
{
for (int i = 0; i < 12; i++)
{
if (low[i] == number)
res += i + 1;
if (high[i] == number)
res += (i + 1) * 13;
}
}
return res;
}
int main()
{
int N;
cin >> N;
getchar(); // 消除回车
for (int i = 0; i < N; i++)
{
string number, translate;
getline(cin, number);
if (isEarth(number))
cout << to_Mars(number) << endl;
else
cout << to_Earth(number) << endl;
}
return 0;
}
上面的在PAT有格式错误,具体没有测试样例,也不知道哪里的问题,下面的代码可以AC。
#include <iostream>
#include <sstream>
using namespace std;
char names[][5] = {
"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec",
"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou",
};
int get(string word)
{
for (int i = 0; i < 25; i ++ )
if (names[i] == word)
{
if (i < 13) return i;
return (i - 12) * 13;
}
return -1; // 一定不会执行
}
int main()
{
int n;
cin >> n;
getchar();
while (n -- )
{
string line;
getline(cin, line);
stringstream ssin(line);
if (line[0] <= '9')
{
int v;
ssin >> v;
if (v < 13) cout << names[v] << endl;
else
{
cout << names[12 + v / 13];
if (v % 13 == 0) cout << endl;
else cout << ' ' << names[v % 13] << endl;
}
}
else
{
int res = 0;
string word;
while (ssin >> word) res += get(word);
cout << res << endl;
}
}
return 0;
}
总结
1. 本题唯一值得总结的是getchar的作用,getchar可以消除行末的空格,从而让getline可以正常接受,而不是第一次收到空白。
2. 其他没啥好说的,就是抠条件。。。。