1044 火星數字 (20 分)
火星人是以 13 進制計數的:
地球人的 0 被火星人稱為 tret,
地球人數字 1 到 12 的火星文分別為:jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec,
火星人將進位以后的 12 個高位數字分別稱為:tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou,
例如地球人的數字 29 翻譯成火星文就是 hel mar;而火星文 elo nov 對應地球數字 115,為了方便交流,請你撰寫程式實作地球和火星數字之間的互譯,
輸入格式:
輸入第一行給出一個正整數 N(<100),隨后 N 行,每行給出一個 [0, 169) 區間內的數字 —— 或者是地球文,或者是火星文,
輸出格式:
對應輸入的每一行,在一行中輸出翻譯后的另一種語言的數字,
輸入樣例:
4
29
5
elo nov
tam
結尾無空行
輸出樣例:
hel mar
may
115
13
結尾無空行
解題思路:
本題有許多注意的地方:
①如果輸入為數字:
當這個數字為13的倍數時,比如:26,轉換為火星數字應為20,按道理而言,答案應為hel tret,但是這里不應該出現tret,因為這個tret只適用于這個所輸入的數字本身為0才輸出tret,其余情況不會含有tret,因此答案應為hel
②當輸入為火星數字時:
常規處理即可,但是有技巧
代碼示例:
#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <string>
#include <cmath>
#include <numeric>
#include <deque>
#include <map>
using namespace std;
int main()
{
int n;
cin >> n;
getchar();
//未進位的火星文
vector<string> low = { "tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec" };
//已進位的火星文
vector<string> hight = { "","tam", "hel", "maa", "huh", "tou", "kes","hei", "elo", "syy", "lok", "mer", "jou" };
while (n--)
{
string s;
getline(cin, s);
if (s[0] >= '0' && s[0] <= '9')
{
int num = stoi(s);
if (num > 13 && num % 13 != 0) //大于13且不為13的倍數時
{
cout << hight[num / 13] << " " << low[num % 13] << endl;
}
else if(num > 0 && num % 13 == 0)//為13的倍數卻不為0時,不輸出末尾的tret
{
cout << hight[num / 13] << endl;
}
else //包含0卻小于13時
{
cout << low[num] << endl;
}
}
else
{
int ans = 0;
if (s.length() > 3)
{
string s1, s2;
s1 = s.substr(0, 3); //取字串s的某個范圍的字串
s2 = s.substr(4); //取字串s下標為4及其以后的字串
for (int i = 0; i < 13; i++)
{
if (s1 == hight[i])
{
ans += i * 13;
}
if (s2 == low[i])
{
ans += i;
}
}
}
else
{
for (int i = 0; i < 13; i++)
{
if (s == hight[i])
{
ans += i * 13;
}
if(s == low[i])
{
ans += i;
}
}
}
cout << ans << endl;
}
}
}
運行結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/291297.html
標籤:其他
