【PAT甲级A1100】1100 Mars Numbers (20分)(c++)

news/2024/12/22 11:02:27/

1100 Mars Numbers (20分)

作者:CHEN, Yue
单位:浙江大学
代码长度限制:16 KB
时间限制:400 ms
内存限制:64 MB

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

题意:

将数字转成13进制的火星文,将火星文转成10进制的数字。

思路:

首先要判断要转换的是数字还是火星文,也不知道火星文的话是两位输入还是1位输入,所以需要读取整行,用第一个字符是否位数字作为判断。在火星文位数的判断上可以采用字符串长度或者找空格。

参考代码:

#include <iostream>
#include <string>using namespace std;
string digit[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string hdigit[13] = {"###", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};void fun1(int a) {if (a / 13)cout << hdigit[a / 13];if (a % 13 && a / 13)cout << " ";if (a % 13 || a == 0)cout << digit[a % 13];
}void fun2(string s) {int a = 0, b = 0;string s1 = s.substr(0, 3), s2;if (s.length() > 4)s2 = s.substr(4);for (int i = 0; i <= 12; i++) {if (s1 == hdigit[i] || s2 == hdigit[i])a = i;if (s1 == digit[i] || s2 == digit[i])b = i;}cout << a * 13 + b;
}int main() {int n;string str;scanf("%d", &n);getchar();for (int i = 0; i < n; i++) {getline(cin, str);if (isdigit(str[0])) {fun1(stoi(str));} else fun2(str);printf("\n");}return 0;
}

如有错误,欢迎指正


http://www.ppmy.cn/news/579711.html

相关文章

pat甲级A1100

作者&#xff1a;王是王小明&#xff0c;仅用来记录自己的学习型经历&#xff0c;转载请表明出处 Actually this coding is not difficult ,the logic is very simple.Because the data volume is small,so the use of the STL’s map directly to establish a mapping table,…

A1100.Mars Numbers

A1100.Mars Numbers //打表进制转换 //10进制转为13进制&#xff0c;只不过13进制的输出方式与常规形式不同&#xff0c;这里采用的是火星文。输入的数据最大只到169&#xff0c; //也就是说对应的13进制最多只有两位&#xff0c;所以不用使用数组存储每个转化后进制位&#x…

A1100 Mars Numbers

题目描述 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 hi…

PAT_A1100

AC代码&#xff1a; #include <iostream> #include <cstdio> #include <map> #include <string> #include <vector> using namespace std;enum v1{jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec}; enum v2{tam, hel, maa, huh, t…

PAT 甲级 A1100

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 t…

PAT甲级 A1100

PAT甲级 A1100 题目详情 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, res…

PAT A1100

实质上没什么难的&#xff0c;体感表达的意思无非是将十进制数字转换为13进制&#xff0c;然后给出将数字替换成相应的字符串&#xff1b; 但是这道题的示例代码给出了所谓打表的新思路&#xff0c;之前没参加过ACM或者OJ比赛&#xff0c;也就没听说过&#xff1b;简单的来说就…

A1100 Mars Numbers [map映射]

貌似cin完之后&#xff1b;读入的光标实际上是停在cin读入的c的后面&#xff1b;这时如果是用cin.get或cin.getline或getchar之类的东西再读入&#xff0c;就会吃进c后面的回车&#xff1b;而如果用cin读入&#xff0c;因为cin本身是会略过回车读入的&#xff0c;所以对cin不会…