PAT甲级1100 Mars Numbers (20分) string转int getline整行读取

1100 Mars Numbers (20分)

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.
Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<100). Then N lines follow, each contains a number in [0, 169), given either in the form of an Earth number, or that of Mars.
Output Specification:

For each number, print in a line the corresponding number in the other language.
Sample Input:

4
29
5
elo nov
tam

Sample Output:

hel mar
may
115
13

思路:很简单,但是我写出来的代码贼长。。。。哎
如果是数字(使用isdigit(第一个字符)来判断),那就是数字转字母,对数字提取十位和个位,另外要注意,如果十位是0 ,那就是小于13的数字,反之就是大于13的数字,但是大于13的数字如果个位为0(也就是13的整数),只输出十位对应的字母

如果是字母,那就是字母转数字,先直接使用string的长度来判断有几个字母,因为一个字母最长也就4位,两个字母肯定是有个位 有十位,但一个字母就不一定只有十位了,因为13的整数倍也是输出一个字母

注意点,因为有hel mar的输出,所以使用getline读取,然后如果整数使用了cin,需要再使用一次getline读取换行符
string转int使用 atoi(string.c_str()) 或者 stoi(str[i]);

进阶版还是去柳神博客:https://blog.csdn.net/liuchuo/article/details/51994339?utm_source=blogxgwz2

代码

#include 
#include 
using namespace std;

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



int main()
{
    int N;
    cin>>N;

    string str[N];
    getline(cin,str[0]);
    for(int i=0; i4)
            {
                //两个字母

                string get1,get2;
                int ge,shi;
                int pos=0;
                for(int m=0; m

你可能感兴趣的:(算法)