pat练习

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 <cctype>//判断字符类型需要的头文件
#include<iostream>
#include <sstream>
using namespace std;

int StringToNum(string str)
{
    int num = 0;
    int Ten = 1;
    for (int i = 0; i < str.length(); i++)
    {
        char n;
        n = str[str.length() - 1 - i];
        switch (n)
        {
        case '0':num = 0 * Ten + num; Ten = Ten * 10; continue;;
        case '1':num = 1 * Ten + num; Ten = Ten * 10; continue;
        case '2':num = 2 * Ten + num; Ten = Ten * 10; continue;
        case '3':num = 3 * Ten + num; Ten = Ten * 10; continue;
        case '4':num = 4 * Ten + num; Ten = Ten * 10; continue;
        case '5':num = 5 * Ten + num; Ten = Ten * 10; continue;
        case '6':num = 6 * Ten + num; Ten = Ten * 10; continue;
        case '7':num = 7 * Ten + num; Ten = Ten * 10; continue;
        case '8':num = 8 * Ten + num; Ten = Ten * 10; continue;
        case '9':num = 9 * Ten + num; Ten = Ten * 10; continue;
        }

    }
    return num;

}
string MarsTonum(string mars)
{
    int Num = 0;
    string strNum;
    if (mars.length() > 4)//两位数 
    {
        string ten = mars.substr(0, 3);
        if (ten == "tam")
        {
            Num = 1 * 13;
        }
        else    if (ten == "hel")
        {
            Num = 2 * 13;
        }
        else    if (ten == "maa")
        {
            Num = 3 * 13;
        }
        else    if (ten == "huh")
        {
            Num = 4 * 13;
        }
        else    if (ten == "tou")
        {
            Num = 5 * 13;
        }
        else    if (ten == "kes")
        {
            Num = 6 * 13;
        }
        else    if (ten == "hei")
        {
            Num = 7 * 13;
        }
        else    if (ten == "elo")
        {
            Num = 8 * 13;
        }
        else    if (ten == "syy")
        {
            Num = 9 * 13;
        }
        else    if (ten == "lok")
        {
            Num = 10 * 13;
        }
        else    if (ten == "mer")
        {
            Num = 11 * 13;
        }
        else    if (ten == "jou")
        {
            Num = 12 * 13;
        }
        string ge = mars.substr(4, 4);
        if (ge == "tret")
        {
            Num += 0;
        }
        else    if (ge == "jan")
        {
            Num += 1;
        }
        else    if (ge == "feb")
        {
            Num += 2;
        }
        else    if (ge == "mar")
        {
            Num += 3;
        }
        else    if (ge == "apr")
        {
            Num += 4;
        }
        else    if (ge == "may")
        {
            Num += 5;
        }
        else    if (ge == "jun")
        {
            Num += 6;
        }
        else    if (ge == "jly")
        {
            Num += 7;
        }
        else    if (ge == "aug")
        {
            Num += 8;
        }
        else    if (ge == "sep")
        {
            Num += 9;
        }
        else    if (ge == "oct")
        {
            Num += 10;
        }
        else    if (ge == "nov")
        {
            Num += 11;
        }
        else    if (ge == "dec")
        {
            Num += 12;
        }
    }
    else//一位数 
    {
        if (mars == "tret")
        {
            Num += 0;
        }
        else    if (mars == "jan")
        {
            Num += 1;
        }
        else    if (mars == "feb")
        {
            Num += 2;
        }
        else    if (mars == "mar")
        {
            Num += 3;
        }
        else    if (mars == "apr")
        {
            Num += 4;
        }
        else    if (mars == "may")
        {
            Num += 5;
        }
        else    if (mars == "jun")
        {
            Num += 6;
        }
        else    if (mars == "jly")
        {
            Num += 7;
        }
        else    if (mars == "aug")
        {
            Num += 8;
        }
        else    if (mars == "sep")
        {
            Num += 9;
        }
        else    if (mars == "oct")
        {
            Num += 10;
        }
        else    if (mars == "nov")
        {
            Num += 11;
        }
        else    if (mars == "dec")
        {
            Num += 12;
        }
        else    if (mars == "tam")
        {
            Num = 1 * 13;
        }
        else    if (mars == "hel")
        {
            Num = 2 * 13;
        }
        else    if (mars == "maa")
        {
            Num = 3 * 13;
        }
        else    if (mars == "huh")
        {
            Num = 4 * 13;
        }
        else    if (mars == "tou")
        {
            Num = 5 * 13;
        }
        else    if (mars == "kes")
        {
            Num = 6 * 13;
        }
        else    if (mars == "hei")
        {
            Num = 7 * 13;
        }
        else    if (mars == "elo")
        {
            Num = 8 * 13;
        }
        else    if (mars == "syy")
        {
            Num = 9 * 13;
        }
        else    if (mars == "lok")
        {
            Num = 10 * 13;
        }
        else    if (mars == "mer")
        {
            Num = 11 * 13;
        }
        else    if (mars == "jou")
        {
            Num = 12 * 13;
        }
    }

    if (Num >= 100)//百位 
    {
        switch (Num / 100)
        {

        case 1:strNum += "1"; break;
        case 2:strNum += "2"; break;
        case 3:strNum += "3"; break;
        case 4:strNum += "4"; break;
        case 5:strNum += "5"; break;
        case 6:strNum += "6"; break;
        case 7:strNum += "7"; break;
        case 8:strNum += "8"; break;
        case 9:strNum += "9"; break;
        }
    }
    if (Num >= 10)//十位 
    {
        switch (Num % 100 / 10)
        {
        case 0:strNum += "0"; break;
        case 1:strNum += "1"; break;
        case 2:strNum += "2"; break;
        case 3:strNum += "3"; break;
        case 4:strNum += "4"; break;
        case 5:strNum += "5"; break;
        case 6:strNum += "6"; break;
        case 7:strNum += "7"; break;
        case 8:strNum += "8"; break;
        case 9:strNum += "9"; break;
        }
    }
    switch (Num % 10)//个位 
    {
    case 0:strNum += "0"; break;
    case 1:strNum += "1"; break;
    case 2:strNum += "2"; break;
    case 3:strNum += "3"; break;
    case 4:strNum += "4"; break;
    case 5:strNum += "5"; break;
    case 6:strNum += "6"; break;
    case 7:strNum += "7"; break;
    case 8:strNum += "8"; break;
    case 9:strNum += "9"; break;
    }


    return strNum;
}
string NumToMars(string num)
{
    /*地球人的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。*/
    string mars;
    int temp = StringToNum(num);
    if (temp / 13)
    {
        if (temp % 13 == 0)
        {
            switch (temp / 13)
            {
            case 0:mars += "tret"; break;
            case 1:mars += "tam"; break;
            case 2:mars += "hel"; break;
            case 3:mars += "maa"; break;
            case 4:mars += "huh"; break;
            case 5:mars += "tou"; break;
            case 6:mars += "kes"; break;
            case 7:mars += "hei"; break;
            case 8:mars += "elo"; break;
            case 9:mars += "syy"; break;
            case 10:mars += "lok"; break;
            case 11:mars += "mer"; break;
            case 12:mars += "jou"; break;
            }
        }
        else
        {
            switch (temp / 13)
            {
            case 0:mars += "tret "; break;
            case 1:mars += "tam "; break;
            case 2:mars += "hel "; break;
            case 3:mars += "maa "; break;
            case 4:mars += "huh "; break;
            case 5:mars += "tou "; break;
            case 6:mars += "kes "; break;
            case 7:mars += "hei "; break;
            case 8:mars += "elo "; break;
            case 9:mars += "syy "; break;
            case 10:mars += "lok "; break;
            case 11:mars += "mer "; break;
            case 12:mars += "jou "; break;
            }
        }

    }
    if (temp % 13 != 0||temp==0)
    {
        switch (temp % 13)
        {
        case 0:mars += "tret"; break;
        case 1:mars += "jan"; break;
        case 2:mars += "feb"; break;
        case 3:mars += "mar"; break;
        case 4:mars += "apr"; break;
        case 5:mars += "may"; break;
        case 6:mars += "jun"; break;
        case 7:mars += "jly"; break;
        case 8:mars += "aug"; break;
        case 9:mars += "sep"; break;
        case 10:mars += "oct"; break;
        case 11:mars += "nov"; break;
        case 12:mars += "dec"; break;
        }
    }
    
    return mars;
}
int main()
{



    int count = 0;
    cin >> count;

    string cinNum;
    //getline(cin, cinNum);//读走换行
    cin.get();
    string coutArr[100];
    for (int i = 0; i < count; i++)
    {
        getline(cin, cinNum);
        if (isdigit(cinNum[0]))//判断字符是否是数字
        {
            coutArr[i] = NumToMars(cinNum);
        }
        else
        {
            coutArr[i] = MarsTonum(cinNum);
        }
    }

    for (int i = 0; i < count; i++)
    {
        cout << coutArr[i] << endl;
    }


    return 0;
}

边界问题 没考虑好---------费了好长时间-----习惯太差劲

原文地址:https://www.cnblogs.com/linxuemufeng/p/8520241.html