zoukankan      html  css  js  c++  java
  • 1100 Mars Numbers (20 分)

    各种乱搞过的=_=。

    string mp[2][13]={
    {"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"},
    {"","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"}
    };
    
    int main()
    {
        int T;
        cin>>T;
        cin.ignore();
    
        while(T--)
        {
            string s;
            getline(cin,s);
    
            if(isdigit(s[0]))
            {
                vector<int> res;
                int x=stoi(s);
                
                if(x == 0) res.pb(0);
                while(x)
                {
                    res.pb(x%13);
                    x/=13;
                }
                
                if(res.size() == 2)
                    cout<<mp[1][res[1]];
                if((res.size() == 2 && res[0] == 0))
                    cout<<endl;
                else if(res.size() == 2)
                    cout<<' '<<mp[0][res[0]]<<endl;
                else
                    cout<<mp[0][res[0]]<<endl;
            }
            else
            {
                int pos=s.find(' ');
                int res=0;
                if(pos != string::npos)
                {
                    string t=s.substr(0,pos);
                    int index1=0;
                    for(int i=0;i<13;i++)
                        if(mp[1][i] == t)
                        {
                            index1=i;
                            break;
                        }
                    res+=index1*13;
                    s=s.substr(pos+1);
                }
    
                int index0=-1;
                for(int i=0;i<13;i++)
                    if(mp[0][i] == s)
                    {
                        index0=i;
                        break;
                    }
    
                if(index0 == -1)
                {
                    int index1=0;
                    for(int i=0;i<13;i++)
                        if(mp[1][i] == s)
                        {
                            index1=i;
                            break;
                        }
                    res+=index1*13;
                }
                else
                    res+=index0;
                cout<<res<<endl;
            }
        }
        //system("pause");
        return 0;
    }
    

    可读性太差,觉得按照晴神思路重写一遍:
    直接针对给出的输入进行模拟会相对复杂,考虑到数据范围最多不超过168,因此不妨将[0,168]的所有数都预处理出来(即打表),然后查询一个输出一个即可。这比直接通过输入来模拟要简单得多。

    然后考虑对个位为[0,12]、十位为0的数与十位为[0,12]、个位为0的数进行特殊处理。

    显然,对个位为[0,12]、十位为0的数来说,就是“tret” ~ “dec”,因此建立[0,12]与字符串的映射即可(字符串到数字的映射使用map<string, int>);对十位为[0,12]、个位为0的数来说,它们是13的倍数,也就是“tret” ~ “jou”,因此建立13的[0,12]倍与字符串的映射即可。

    接着考虑个位和十位均不为0的数,根据题意,这些数可以直接通过十位的火星文拼接上个位的火星文得到。

    注意点

    13的倍数不应当输出个位的“tret”,例如13应当输出“tam” 而不是“tam tret”。

    代码

    非常清爽。

    string mp[2][13]={
    {"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"},
    {"tret","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"}
    };
    string numtostr[170];
    map<string,int> strtonum;
    
    void init()
    {
        for(int i=0;i<13;i++)
            for(int j=0;j<13;j++)
            {
                string str;
                if(i == 0) str=mp[0][j];
                else if(j == 0) str=mp[1][i];
                else str=mp[1][i]+' '+mp[0][j];
                numtostr[i*13+j]=str;
                strtonum[str]=i*13+j;
            }
    }
    
    int main()
    {
        init();
    
        int T;
        cin>>T;
        cin.ignore();
    
        while(T--)
        {
            string s;
            getline(cin,s);
            if(isdigit(s[0]))
            {
                int res=0;
                for(int i=0;i<s.size();i++)
                    res=res*10+(s[i]-'0');
                cout<<numtostr[res]<<endl;
            }
            else 
                cout<<strtonum[s]<<endl;
        }
        //system("pause");
        return 0;
    }
    
  • 相关阅读:
    connot connect to mysql 10061
    <context:annotation-config/>,<mvc:annotation-driven/>和<context:component-scan>之间的关系
    Failed to read artifact descriptor for xxx:jar的问题解决
    URI is not registered ( Setting | Project Settings | Schemas and DTDs )
    MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring
    springmvc.xml,context.xml和web.xml
    web.xml
    Spring配置文件XML详解
    springmvc配置文件
    Android设置窗口、控件透明度
  • 原文地址:https://www.cnblogs.com/fxh0707/p/14428862.html
Copyright © 2011-2022 走看看