1100 Mars Numbers 火星数字(抠条件)

该程序用于在地球数字系统(0-12,基于10的进制)和火星数字系统(0-tret,1-jan到12-dec,13-tam到24-jou,基于13的进制)之间进行翻译。输入包含一个正整数N和N个数字,程序会将每个数字转换成另一种系统的表示。
摘要由CSDN通过智能技术生成

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
#include <iostream>
using namespace std;
const string low[12] = {"jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
const string high[12] = {"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};

bool isEarth(string number)
{
  if (number[0] >= '0' && number[0] <= '9')
    return true;
  return false;
}

string to_Mars(string earth)
{
  string res;
  int number = stoi(earth);
  if (!number)
    return "tret";
  if (number % 13)
    res = low[number % 13 - 1] + res;
  if (number / 13)
    res = high[number / 13 - 1] + " " + res;
  return res;
}

int to_Earth(string number)
{
  int res = 0;
  if (number.size() == 7)
  {
    string l;
    string h;
    for (int i = 0; i < 3; i++)
    {
      h = h + number[i];
      l = l + number[i + 4];
    }
    for (int i = 0; i < 12; i++)
      if (low[i] == l)
        res += i + 1;
    for (int i = 0; i < 12; i++)
      if (high[i] == h)
        res += (i + 1) * 13;
  }
  else
  {
    for (int i = 0; i < 12; i++)
    {
      if (low[i] == number)
        res += i + 1;
      if (high[i] == number)
        res += (i + 1) * 13;
    }
  }
  return res;
}

int main()
{
  int N;
  cin >> N;
  getchar(); // 消除回车
  for (int i = 0; i < N; i++)
  {
    string number, translate;
    getline(cin, number);
    if (isEarth(number))
      cout << to_Mars(number) << endl;
    else
      cout << to_Earth(number) << endl;
  }

  return 0;
}

上面的在PAT有格式错误,具体没有测试样例,也不知道哪里的问题,下面的代码可以AC。

#include <iostream>
#include <sstream>

using namespace std;

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

int get(string word)
{
    for (int i = 0; i < 25; i ++ )
        if (names[i] == word)
        {
            if (i < 13) return i;
            return (i - 12) * 13;
        }
    return -1;  // 一定不会执行
}

int main()
{
    int n;
    cin >> n;
    getchar();

    while (n -- )
    {
        string line;
        getline(cin, line);

        stringstream ssin(line);
        if (line[0] <= '9')
        {
            int v;
            ssin >> v;
            if (v < 13) cout << names[v] << endl;
            else
            {
                cout << names[12 + v / 13];
                if (v % 13 == 0) cout << endl;
                else cout << ' ' << names[v % 13] << endl;
            }
        }
        else
        {
            int res = 0;
            string word;
            while (ssin >> word) res += get(word);
            cout << res << endl;
        }
    }

    return 0;
}

总结

1. 本题唯一值得总结的是getchar的作用,getchar可以消除行末的空格,从而让getline可以正常接受,而不是第一次收到空白。

2. 其他没啥好说的,就是抠条件。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值