1. 程式人生 > >PAT 乙級 1044 火星數字

PAT 乙級 1044 火星數字

cout tro src etom getc 只有一個 str ans block

技術分享圖片

輸入樣例:
4
29
5
elo nov
tam
輸出樣例:
hel mar
may
115
13

思路是:

  • 建立兩張參照表,分別對應高位火星文和低位火星文
  • 若需要轉換到火星文,則首先將地球文字轉化為十三進制數,通過查表翻譯成火星文
  • 若需要轉換到地球文字,則首先要將火星文通過查表轉化成十三進制數,再轉化十進制數,完成翻譯。

需要註意的是:

  • 由於十三進制數可能為 1211 高位為12,低位為11,直接用一個整型變量儲存會導致無法分辨出低位和高位導致無法正確翻譯成火星文,所以推薦分別用兩個變量儲存高位和低位。
  • 如果存在輸入的火星文只包含一個高位,例如 tam ,為高位的數字 13 ,這是要做好判斷,因為有時我們會認為如果只有一個火星文字就代表只有個位。
  • 如果輸入的地球文翻譯成火星文的時候高位不為 0 且低位為0,這時無需輸出低位對應的火星文,而只輸出高位對應的火星文。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

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

int toTen(int high,int low);
int* toThirteen(int x);
int translateToEarth(const string& s);
string translateToMars(int x);
void translate(char** p,int n);

int main()
{
    int n;
    cin>>n;
    char** p=new char*[n];
    for(int i=0; i<n; i++)
        p[i]=new char[10];
    getchar();
    for(int i=0; i<n; i++)
    {
        scanf("%[^\n]",p[i]);
        getchar();
    }
    translate(p,n);
    delete p;
    return 0;
}

int* toThirteen(int x)
{
    int *p=new int[2];
    p[0]=x%13;
    p[1]=x/13;
    return p;
}

int toTen(int high,int low)
{
    return high*13+low;
}

string translateToMars(int x)
{
    int* p=toThirteen(x);
    if(p[1]!=0)
    {
        if(p[0]!=0)
            return dictionariesHigh[p[1]]+" "+dictionariesLow[p[0]];
        else
            return dictionariesHigh[p[1]];
    }
    else
        return dictionariesLow[p[0]];
    delete p;
}

int translateToEarth(const string& s)
{
    int high=0,low=0;
    if(s.find(‘ ‘)!=string::npos)
    {
        for(int i=1; i<13; i++)
        {
            if(s.substr(0,s.find(‘ ‘))==dictionariesHigh[i])
            {
                high=i;
                break;
            }
        }
        for(int i=0; i<13; i++)
        {
            if(s.substr(s.find(‘ ‘)+1)==dictionariesLow[i])
            {
                low=i;
                break;
            }
        }
    }
    else
    {
        bool isOnlyLow=true;
        for(int i=1; i<13; i++)
        {
            if(s==dictionariesHigh[i])
            {
                isOnlyLow=false;
                high=i;
                break;
            }
        }
        if(isOnlyLow)
        {
            for(int i=1; i<13; i++)
            {
                if(s==dictionariesLow[i])
                {
                    low=i;
                    break;
                }
            }
        }
    }
    return toTen(high,low);
}

void translate(char** p,int n)
{
    int x=0;
    for(int i=0; i<n-1; i++)
    {
        x=0;
        if(p[i][0]>=‘0‘&&p[i][0]<=‘9‘)
        {
            for(int j=0; j<strlen(p[i]); j++)
                x=x*10+(p[i][j]-‘0‘);
            cout<<translateToMars(x)<<endl;
        }
        else
            cout<<translateToEarth(string(p[i]))<<endl;
    }
    x=0;
    if(p[n-1][0]>=‘0‘&&p[n-1][0]<=‘9‘)
    {
        for(int j=0; j<strlen(p[n-1]); j++)
            x=x*10+(p[n-1][j]-‘0‘);
        cout<<translateToMars(x);
    }
    else
        cout<<translateToEarth(string(p[n-1]));
}

PAT 乙級 1044 火星數字