PAT---1100 Mars Numbers

解题思路:设置两个字符串数组,list和base,其中list数组中存放的是13的倍数,分别是13的1倍, 2倍,3倍。。。

#include <iostream>
#include <string>

using namespace std;

string list[12] = {"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
string base[13] = {"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};


int main()
{
    int n;
    string str;
    while(cin >> n)
    {
        getchar();
        for(int j = 0; j < n; j++)
        {
            getline(cin, str);
            if(str.find(' ') != -1)
            {
                int ans = 0;
                string str1 = str.substr(0, str.find(' '));
                string str2 = str.substr(str.find(' ')+1);
                for(int i = 0; i < 12; i++)
                    if(list[i] == str1)
                    {
                        ans = (i+1)*13;
                        break;
                    }
                for(int i = 0; i < 13; i++)
                    if(base[i] == str2)
                    {
                        ans += i;
                        break;
                    }
                cout << ans << endl;
            }
            else
            {
                if(isdigit(str[0]))
                {
                    int tmp = stoi(str);
                    int k = tmp % 13;
                    tmp /= 13;
                    if(tmp == 0 && k == 0)
                        cout << "tret" << endl;
                    else if(tmp != 0 && k == 0)   //13的倍数
                        cout << list[tmp-1] << endl;
                    else if(tmp == 0 && k != 0)
                        cout << base[k] << endl;
                    else
                        cout << list[tmp-1] << " " << base[k] << endl;
                }
                else
                {
                    int ans = 0;
                    for(int i = 0; i < 12; i++)
                        if(list[i] == str)
                        {
                            cout << 13*(i+1) << endl;
                            break;
                        }
                    for(int i = 0; i < 13; i++)
                        if(base[i] == str)
                        {
                            cout << i << endl;
                            break;
                        }
                }
            }
        }
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/mch2869253130/article/details/88019698