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 <iostream>
#include <string>
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; i<N; i++)
    {
        getline(cin,str[i]);
    }
    for(int i=0; i<N; i++)
    {
        if(isdigit(str[i].at(0)))
        {
            //数字转字母
            int chuli=stoi(str[i]);
            int shi,ge;
            shi=chuli/13;
            ge=chuli%13;

            if(shi==0)
            {
                cout<<a[ge]<<endl;
            }
            else
            {
                if(ge==0)
                    cout<<b[shi]<<endl;
                else{
                    cout<<b[shi]<<' ';
                cout<<a[ge]<<endl;
                }
            }
        }
        else
        {
            string chuli=str[i];
            if(str[i].size()>4)
            {
                //两个字母

                string get1,get2;
                int ge,shi;
                int pos=0;
                for(int m=0; m<str[i].size(); m++)
                {
                    if(str[i][m]==' ')
                        pos=m;


                }
                get1=chuli.substr(0,pos);
                for(int m=1; m<13; m++)
                {
                    if(b[m]==get1)
                    {
                        shi=m;
                    }
                }

                get2=chuli.substr(pos+1,str[i].size());
                for(int m=0; m<13; m++)
                {
                    if(a[m]==get2)
                    {
                        ge=m;
                    }
                }
                cout<<shi*13+ge<<endl;
            }
            else
            {
                int ge=-1;
                for(int m=0; m<13; m++)
                {
                    if(a[m]==str[i])
                    {
                        ge=m;
                    }
                }
                if(ge!=-1)
                    cout<<ge<<endl;
                else
                {
                    for(int m=0; m<13; m++)
                    {
                        if(b[m]==str[i])
                        {
                            ge=m;
                        }
                    }
                    cout<<ge*13<<endl;
                }

            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值