思路是分成输入地球数字和火星数字两种情况,通过读入字符串判断首位即可,但需要注意的是字符串的读入,因为火星数字中间可能有空格,所以必须用getline获取字符串。而在使用getline前要先清空cin流,即
cin.clear();
cin.ignore();
而且ignore()会跳过读入一个值,故这两句应写在for循环之前。
1044. 火星数字(20)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
火星人是以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
#include<iostream> #include<algorithm> #include<iomanip> #include<cstring> #include<vector> #include<stdio.h> #include<math.h> #include<string> #include<sstream> using namespace std; int main(){ string l[]={"jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"}; string h[]={"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"}; int n; cin>>n; cin.clear(); cin.ignore(); string s;int z=0;int y=0; for(int i=0;i<n;i++){ getline(cin,s); if(s[0]>='0'&&s[0]<='9'){ int temp=0; for(int j=0;j<s.length();j++) temp=(s[j]-'0')+temp*10; z=temp/13; y=temp%13; if(z!=0)cout<<h[z-1]; if(z!=0 && y!=0)cout<<" "<<l[y-1]; if(z==0 && y!=0)cout<<l[y-1]; if(z==0 && y==0)cout<<"tret"; if(i!=n-1)cout<<endl; } if(s[0]>='a'&&s[0]<='z') { int temp=0; string s1=s.substr(0,3); if(s.length()>4){ string s2=s.substr(4,3); for(int j=0;j<12;j++){ if(s2==l[j]) temp+=(j+1); if(s1==h[j]) temp+=(j+1)*13; } if(i!=n-1)cout<<temp<<endl; else cout<<temp; }else{ if(s1=="tret"){ if(i!=n-1)cout<<0<<endl; else cout<<0; } else{ for(int j=0;j<12;j++){ if(s1==l[j]) temp=j+1; if(s1==h[j]) temp=(j+1)*13; } if(i!=n-1)cout<<temp<<endl; else cout<<temp; } } } } return 0; }