美文网首页
1100 Mars Numbers

1100 Mars Numbers

作者: Skittle丶 | 来源:发表于2020-11-07 22:16 被阅读0次

    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.
    意思就是说,人们在火星上把1、2、3……叫做"jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec",同时把13的1倍,13的二倍叫做"tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou",其实就是类似于进制转换嘛。
    算法笔记上写的感觉太复杂了,还人造hash,感觉没有必要。第二个测试案例总是不通过,不知道为啥。最后在判断

    if(str[0]>='0'&& str[0]<='9'){
                EtoM(str);
            }else{
                MtoE(str);
            }
    

    的时候,str[0]>='0'取了个等于号,第二个测试案例就过了。说真的我觉得也很奇葩,肯定是有05 07这样的数来考验大家了
    附上代码,肯定是AC啦

    #include<iostream>
    #include<string>
    using namespace std;
    int n;
    string low[13]={"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
    string high[13]={"", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
    
    
    int change(string word)
    {
        for(int i = 0; i < 13; i++){//0~13
            if(low[i]==word) return i;
        }
        for(int i = 0; i < 13; i++){//0~13
            if(high[i]==word) return i*13;
        }
    }
    void MtoE(string str){
        string word;
                int ans = 0;
                for(int i = 0; i < str.size(); i++){
                    if(str[i]!=' '){
                        word += str[i];
                    }
                    else {
                        ans += change(word);
                        word = "";
                    }
                }
                ans += change(word);
                printf("%d\n",ans);
    }
    
    void EtoM(string str){
        int cnum=0;
        int slen=str.size();
        for(int i=0;i<slen;i++){
            cnum=cnum*10+str[i]-'0';
        }
        int h=cnum/13;
        int l=cnum%13;
        if(h!=0){
            if(l!=0){
                //printf("%s %s",high[h],low[l]);
                //printf()
                cout<<high[h]<<" "<<low[l]<<endl;
            }else{
                //printf("%s",high[h]);
                cout<<high[h]<<endl;
            }
        }else{
            //printf("%s",low[l]);
            cout<<low[l]<<endl;
        }
    }
    
    int main(){
        scanf("%d",&n);
        getchar();
        for(int i=0;i<n;i++){
            string str;
            getline(cin,str);
            //cout<<str<<endl;
            if(str[0]>='0'&& str[0]<='9'){
                EtoM(str);
            }else{
                MtoE(str);
            }
        }
        
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:1100 Mars Numbers

          本文链接:https://www.haomeiwen.com/subject/zgaqbktx.html