PAT1100 Mars Numbers
People on Mars count their numbers with base 13:
- Zero on Earth is called "tret" on Mars.
- The numbers 1 to 12 on Earch 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
注意點:在c++中輸入帶空格的字串可用cin.getline(str1,len)(str1是字元陣列char[])或getline(cin.str2)(str2是字串型別string)。用getline()函式進行迴圈輸入時,由於cin函式是直接從緩衝區取資料的,所以當緩衝區有殘留資料時,cin函式會直接取得這些殘留資料而不會請求鍵盤輸入。 因此,當用getline()函式輸入之前有資料輸入的話,要將之前的緩衝區內的殘留資料清除。
此題中用getchar()吃掉之前的回車輸入。
AC程式碼:
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
map<int,string> T1,T2;
map<string,int> E1,E2;
int main()
{
T1[0] = "tret";T2[0] = "";
T1[1] = "jan"; T1[2] = "feb"; T1[3] = "mar";
T1[4] = "apr"; T1[5] = "may"; T1[6] = "jun";
T1[7] = "jly"; T1[8] = "aug"; T1[9] = "sep";
T1[10] = "oct"; T1[11] = "nov"; T1[12] = "dec";
T2[1] = "tam"; T2[2] = "hel"; T2[3] = "maa";
T2[4] = "huh"; T2[5] = "tou"; T2[6] = "kes";
T2[7] = "hei"; T2[8] = "elo"; T2[9] = "syy";
T2[10] = "lok"; T2[11] = "mer"; T2[12] = "jou";
E1["jan"] = 1; E1["feb"] = 2; E1["mar"] = 3;
E1["apr"] = 4; E1["may"] = 5; E1["jun"] = 6;
E1["jly"] = 7; E1["aug"] = 8; E1["sep"] = 9;
E1["oct"] = 10; E1["nov"] = 11; E1["dec"] = 12;
E2["tam"] = 1; E2["hel"] = 2; E2["maa"] = 3;
E2["huh"] = 4; E2["tou"] = 5; E2["kes"] = 6;
E2["hei"] = 7; E2["elo"] = 8; E2["syy"] = 9;
E2["lok"] = 10; E2["mer"] = 11; E2["jou"] = 12;
freopen("9.txt","r",stdin);
int N;
scanf("%d",&N);
getchar();
for(int i = 0; i< N; i++){
string p;
getline(cin,p);
if(p[0]>='0'&&p[0]<='9'){
int m = 0, n = 1;
for(int j = p.size() - 1; j>=0; j--){
m += (p[j] - '0')*n;
n = n*10;
}
printf("%s",T2[m/13].c_str());
if(m/13 && m%13) printf(" %s",T1[m%13].c_str());
else if(!(m/13) && m%13) printf("%s",T1[m%13].c_str());
else if(!(m/13) && m%13 == 0) printf("%s",T1[m%13].c_str());
printf("\n");
}else{
int m, n;
if(p.size() > 3){
m = E2[p.substr(0,3)];
n = E1[p.substr(4,3)];
printf("%d\n",m*13 + n);
}else{
if(E1.find(p) != E1.end()){
printf("%d\n",E1[p]);
}else{
printf("%d\n",E2[p]*13);
}
}
}
}
}
相關推薦
PAT1100:Mars Numbers
each mars ati ogr pan resp 進制 lan tle 1100. Mars Numbers (20) 時間限制 400 ms 內存限制 65536 kB 代碼長度限制 16000 B 判題程序 Standard 作者 CHEN, Yu
PAT1100 Mars Numbers
People on Mars count their numbers with base 13: Zero on Earth is called "tret" on Mars. The numbers 1 to 12 on Earch is called "jan, fe
pat 1100 Mars Numbers(20 分)
examples earch clas fir other mutual form Language for 1100 Mars Numbers(20 分) People on Mars count their numbers with base 13: Zero
PAT 1100 Mars Numbers (20 分)
1100 Mars Numbers (20 分) People on Mars count their numbers with base 13: Zero on Earth is called “tret” on Mars. The numbers 1 to 12 on Earch is
PAT 1100 Mars Numbers[難]
1100 Mars Numbers (20 分) People on Mars count their numbers with base 13: Zero on Earth is called "tret" on Mars. The
PAT 1100 Mars Numbers
People on Mars count their numbers with base 13: Zero on Earth is called "tret" on Mars. The numbers 1 to 12 on Earch is called "jan, fe
Codeforces 55D Beautiful numbers(數位dp)
pac urn etc number div clu 能夠 是我 tdi 題目大意:T(<=10)組數據,求[a,b]能夠被其每個數位的數都整除的數(a,b<=9*10^18) 這題差一點就想出來了,可是最後一步好難想也好妙啊 首先這個數能夠整除各個
poj 3252 Round Numbers 數位dp
變量 數量 div read sdn .net blog air ble 題目鏈接: http://poj.org/problem?id=3252 題意: 求一段區間中二進制中0的數量要不能少於1的數量的數的個數。 思路: 套路 都是套路 http://blog.csdn
LeetCode---------Add Two Numbers 解法
eve n-n pty lead http 順序 number sum represent You are given two non-empty linked lists representing two non-negative integers. The digits
[LeetCode] Add Two Numbers
std 轉化 代碼 single ria 多種解法 ret data -m You are given two linked lists representing two non-negative numbers. The digits are stored in
3N Numbers
line following sel constrain positive second turn log cte D - 3N Numbers Time limit : 2sec / Memory limit : 256MB Score : 500 point
[Intermediate Algorithm] - Sum All Odd Fibonacci Numbers
org nac mil 數列 個數字 code target reference ava 題目 給一個正整數num,返回小於或等於num的斐波納契奇數之和。 斐波納契數列中的前幾個數字是 1、1、2、3、5 和 8,隨後的每一個數字都是前兩個數字之和。 例如,sumFib
leetcode鏈表--14、add-two-numbers(兩鏈表相加 得到新鏈表)
logs 錯誤 align 描述 eight val str nodes sent 題目描述 You are given two linked lists representing two non-negative numbers. The digits are sto
UVA 10042 Smith Numbers(數論)
sizeof ret col 保存 進行 uva nal isp published Smith Numbers Background While skimming his phone directory in 1982, Albert Wilansky, a ma
數據可視化入門之show me the numbers
推薦 有趣的 好的 style blank 分享 span 需要 width 數據的可視化一直是自己瞎玩著學,近來想系統的學數據可視化的東西,於是搜索資料時看到有人推薦《show me the numbers》作為入門。 由於搜不到具體的書籍內容,只能搜到一個
Leetcode Add two numbers
current linklist number list main popu val post tail #include <iostream> using namespace std; struct ListNode { int val;
URAL 2031. Overturned Numbers (枚舉)
tin width mes bit imu ger digi all under 2031. Overturned Numbers Time limit: 1.0 second Memory limit: 64 MB Little Pierre was surf
Humble Numbers(醜數) 超詳解!
then 每一個 %d sample 如果 nbsp 輸出 define clu 給定一個素數集合 S = { p[1],p[2],...,p[k] },大於 1 且素因子都屬於 S 的數我們成為醜數(Humble Numbers or Ugly Numbers),記第 n
[LeetCode] 448.Find All Numbers Disappeared in an Array
return htm put lis inpu pear rand ati n) p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #4e9072 } p.p2 { margin: 0.0
LeetCode - 2 - Add Two Numbers
dtw ble bind ber plus binding bsp pan number 題目 URL:https://leetcode.com/problems/add-two-numbers/ 解法 沒什麽特殊的解法,按位相加即可。 註意點: 1、l1 和 l