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 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

思路:

个人觉得真是个好题!考察了好几个知识点!~
1.10进制转d进制和d进制转10进制
2.map的使用,i和value
3.分割单词

注意点:

  1. 10进制转13进制的时候,如13,26,不该转化为tam tret,应该转为tam,并且其中tret前面的空格也要消去!!!,不然会卡测试点2,4格式错误!!
  2. 13进制转10进制的时候,一位数的时候,要判断是否是低位的一位数,还是高位的一位数!

害,真不觉得这像个20分题~

最后上代码~

#include
#include
#include
#include
using namespace std;
map<string,int> b_get_num,a_get_num;
string a[13]={"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string b[13]={"0","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
void to_13(int x){
    vector<int> ans;
    do{
        ans.push_back(x%13);
        x/=13;
    }while(x>0);
    if(ans.size()==1) printf("%s\n",a[ans[0]].c_str());
    else printf("%s%s\n",b[ans[1]].c_str(),a[ans[0]]=="tret"?"":(" "+a[ans[0]]).c_str());
}
void to_10(vector<string> data){
    if(data.size()==1) printf("%d\n",b_get_num[data[0]]==0?a_get_num[data[0]]:b_get_num[data[0]]*13);
    else printf("%d\n",b_get_num[data[0]]*13+a_get_num[data[1]]);
}
bool judge(char c){ return (c>='a'&&c<='z')||(c>='A'&&c<='Z'); }
int main(){
    for(int i=1;i<=12;i++) b_get_num[b[i]]=i;
    for(int i=0;i<=12;i++) a_get_num[a[i]]=i;
    int n;
    cin>>n;
    getchar();
    string temp; int x;
    while(n--){
        getline(cin,temp);
        if(temp[0]>='0'&&temp[0]<='9'){
            x=stoi(temp);
            to_13(x);
        }else{
            vector<string> data;
            string word="";
            for(int i=0;i<temp.size();i++){
                if(judge(temp[i])){
                    word+=temp[i];
                }
                if(!judge(temp[i])||i==temp.size()-1){
                    if(word.size()>0){
                        data.push_back(word);
                        word="";
                    }
                }
            }
            to_10(data);
        }
    }
}

你可能感兴趣的:(PAT甲级)