zoukankan      html  css  js  c++  java
  • 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 Earch 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
    


    这道题来自2015年9月12号刚考完的PAT甲级考试第一题,本人表示被虐成狗T T,第一次考试就碰到这种坑题。

    好了不吐槽了,来说一下这道题的正确做法。

    首先最重要的是第一句,火星上的人以13进制计数,因此如果要将数字转化为火星进制,首先要进行除13取余法进制转换,例如样例中的29在13进制下为23,然后再拿高位和低位分别去查表。

    最容易犯的错误是13的整倍数输出问题,例如39,应该输出maa而不是maa tret。

    这道题的关键是输入判定和字符处理,下面分别讨论。

    ①输入判定:对输入的第一个字符判定,如果是字母,则按照火星进制处理,否则按照地球数字处理。

    ②字符和数字的对转:使用stringstream,设ss是一个stringstream类型的对象,要清空之前转化过的信息,使用ss.clear()即可,如果不放心可以先ss.str("");然后再ss.clear();

    ③带空格字符串的录入:使用getline(cin,str),注意之前如果有cin输入需要getchar()吃掉回车。

    ④字符串分割:如果空格分隔的多个字符串,使用stringstream ss一直输出到字符串,直到ss.fail()==true结束输出,即可得到每一个字符串。

    ⑤字符串转换回数字:使用map记录低位和高位的对应关系,然后查找即可。

    代码如下:

    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <vector>
    #include <string>
    #include <algorithm>
    #include <sstream>
    #include <map>
    
    using namespace std;
    
    string lowTb[] = {"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
    string highTb[] = {"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
    
    int main()
    {
        map<string,int> low2int,high2int,common2int;
        for(int i = 0; i <= 12; i++) low2int[lowTb[i]] = i;
        for(int i = 1; i <= 12; i++) high2int[highTb[i-1]] = i;
        int N;
        stringstream ss1,ss2;
        cin >> N;
        getchar();
        string tmp;
        vector<string> inputs;
        for(int i = 0; i < N; i++){
            getline(cin,tmp);
            inputs.push_back(tmp);
        }
        for(int i = 0; i < inputs.size(); i++){
            string str = inputs[i];
            if(isalpha(str[0])){
                ss1.clear();
                ss1.str(str);
                string seg;
                vector<string> num;
                while(1){
                    ss1 >> seg;
                    if(ss1.fail()) break;
                    num.push_back(seg);
                }
                if(num.size() == 1){
                    if(low2int.find(num[0])!=low2int.end()){
                        printf("%d
    ",low2int[num[0]]);
                    }else{
                        int n = high2int[num[0]] * 13;
                        printf("%d
    ",n);
                    }
                }else{
                    int low = low2int[num[1]];
                    int high = high2int[num[0]];
                    printf("%d
    ",low + high * 13);
                }
            }else{
                // num
                int base = 13;
                int yu;
                ss2.str("");
                ss2.clear();
                ss2 << str;
                int N;
                ss2 >> N;
                vector<int> num;
                if(N <= 12){
                    printf("%s
    ",lowTb[N].c_str());
                    continue;
                }
                while(N){
                    yu = N % base;
                    num.push_back(yu);
                    N /= base;
                }
                if(num.size() == 2){
                    int high = num[1];
                    int low = num[0];
                    if(low != 0){
                        printf("%s %s
    ",highTb[high-1].c_str(),lowTb[low].c_str());
                    }else{
                        printf("%s
    ",highTb[high-1].c_str());
                    }
                }else{
                    int low = num[0];
                    printf("%s
    ",lowTb[low].c_str());
                }
            }
        }
        return 0;
    }
    


  • 相关阅读:
    telent

    linux系统下部署war包
    CentOS-7.0.中安装与配置Tomcat-7的方法
    ServletContextListener
    JAVA中静态块、静态变量加载顺序详解
    git 查看父分支
    Google Guice 之绑定1
    STM32 Cubemx 配置定时器定时1mS
    项目过程的几点经验总结
  • 原文地址:https://www.cnblogs.com/aiwz/p/6154031.html
Copyright © 2011-2022 走看看