zoukankan      html  css  js  c++  java
  • PAT1100:Mars Numbers

    1100. Mars Numbers (20)

    时间限制
    400 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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

    思路

    进制转换和字符串处理。
    1.分为地球数字转火星文、火星文转地球数字两种情况。
    2.火星文转地球数字分为三种情况:1)单独一个火星数字 2)两个火星数字(第二个火星数字不是tret) 3)两个火星数字(第二个火星数字一定是tret(零))。

    代码
    #include<iostream>
    #include<vector>
    #include<string>
    using namespace std;
    vector<string> num = {"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
    vector<string> highernum = {"sb","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok","mer", "jou"};
    
    
    int search(string s)
    {
        for(int i = 0;i < num.size();i++)
        {
            if(s == num[i])
                return i;
        }
    
        for(int i = 0;i < highernum.size();i++)
        {
            if(s == highernum[i])
                return -i;
        }
        return 0;
    }
    
    int main()
    {
        int N;
        scanf("%d",&N);
        getchar();
           while(N--)
           {
               string number;
               getline(cin,number);
               if(number[0] <= '9')
               {
                  int n = stoi(number);
                  int first = n/13,second = n % 13;
                  if(n == 0)
                    cout << num[n] <<endl;
                  else
                  {
                    if(first !=0 && second != 0)
                        cout << highernum[first]<< " " <<num[second] << endl;
                    else if(first != 0)
                      cout << highernum[first] << endl;
                    else if(second != 0)
                      cout << num[second] << endl;
                  }
    
    
               }
               else if(number.size() == 3)
               {
                    int res = search(number);
                    if(res >= 0)
                      cout << res <<endl;
                    else
                      cout << -res * 13 << endl;
               }
               else if(number.size() == 7)
               {
                   string fst = number.substr(0,3);
                   string sec = number.substr(4,3);
                   cout << -search(fst) * 13 + search(sec) << endl;
               }
               else
               {
                   string fst = number.substr(0,3);
                    cout << -search(fst) * 13 << endl;
               }
           }
    }
  • 相关阅读:
    NIO[读]、[写]在同一线程(单线程)中执行,让CPU使用率最大化,提高处理效率
    解码(ByteBuffer): CharsetDecoder.decode() 与 Charset.decode() 的不同
    ACCESS与SQL Server下SQL Like 查询的不同
    试着用c写了一个多线程的同步
    收藏:c语言的多线程同步
    java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] 不能使用 '(未知的)';文件已在使用中
    Linux进程管理
    MySQL数据备份
    MySQL索引
    Socket.IO 概述
  • 原文地址:https://www.cnblogs.com/0kk470/p/7646783.html
Copyright © 2011-2022 走看看