您的位置:首页 > 其它

PAT甲级 1100. Mars Numbers (20)

2017-01-19 17:20 441 查看
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
题目大意
有两种语言的表示:地球数字和火星数字[0, 169)
火星数字的表示是13进制,“0-12”是"tret, jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec"。“13的倍数”为"tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou"。
输入n个数据(火星文或地球文),输出另外一种语言的表达形式。

题目解析
1、火星文转化为地球数字时:火星文最多只有两位,可以根据长度来判断,然后单独处理高位和低位的表示。
2、地球数字转化为火星文时;先转换为13进制,高位用火星文中“13的倍数”表示,低位则用1-12表示。注意这里当高位时13的倍数时,低位的0时不输出的。只有高位和地位都有数据是才有中间的空格输出。

#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <
4000
;set>
#include <queue>
#include <stack>
#include <cstdlib>
#include <climits>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define ll long long
const int MAXN = 100000 + 5;
const int MAXM = 100000 + 5;
const int INF = 0x7f7f7f7f;
const int dir[][2] = {0,1,1,0,0,-1,-1,0};
template <class XSD> inline XSD f_min(XSD a, XSD b) { if (a > b) a = b; return a; }
template <class XSD> inline XSD f_max(XSD a, XSD b) { if (a < b) a = b; return a; }

int n;
char s[33], mar_ten[20][4]={"", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};///火星高位
char mar_bi[20][5]={"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};///火星低位
void Output_mar(int ten, int bi){///输出火星文
bool flag=true;
if(ten) printf("%s", mar_ten[ten]), flag=false;
if(ten!=0 && bi!=0) printf(" ");
if(bi) printf("%s", mar_bi[bi]);
if(!ten && !bi) printf("%s", mar_bi[0]);
printf("\n");
}

void Solve(){
int len=strlen(s);
if(s[0]>='0' && s[0]<='9'){///地球数字
int num=0;
for(int i=0; i<len; i++) num = num*10 + s[i]-'0';
Output_mar(num/13, num%13);
}
else{///火星文
if(len==3||len==4){///火星文只有一位的
for(int i=0; i<13; i++){
bool flag=true;
for(int j=0; j<len; j++) if(mar_bi[i][j]!=s[j]) flag=false;
if(flag){printf("%d\n", i);break;}
}
for(int i=0; i<13; i++){
bool flag=true;
for(int j=0; j<len; j++) if(mar_ten[i][j]!=s[j]) flag=false;
if(flag){printf("%d\n", i*13);break;}
}
}else{///火星文有两位
int sum=0;
for(int i=0; i<13; i++){///前一位
bool flag=true;
for(int j=0; j<3; j++) if(mar_ten[i][j]!=s[j]) flag=false;
if(flag){sum += i*13; break;}
}
for(int i=1; i<13; i++){///后一位
bool flag=true;
for(int j=0; j<3; j++) if(mar_bi[i][j]!=s[j+4]) flag=false;
if(flag){sum+=i; break;}
}
printf("%d\n", sum);
}
}
}
void Getdata(){
getchar();
while(n--){
gets(s);
Solve();
}
}
int main(){
while(~scanf("%d", &n)){
Getdata();
///Solve();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: