PTA甲级 1100 Mars Numbers (20分)


强烈推荐,刷PTA的朋友都认识一下柳神–PTA解法大佬

本文由参考于柳神博客写成

柳神的CSDN博客,这个可以搜索文章

柳神的个人博客,这个没有广告,但是不能搜索

PS 今天也要加油鸭

在这里插入图片描述

题目原文

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

生词如下:

respectively 分别的

题目大意:

第一句话

火星上的进制是13 —吐槽,我第一句话,就理解错了.我以为是月份是13

计数他们的数字,用13 很合理.

0 在月球上是tret来表示的

地球上的12进制是 “jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec”

然后高位的数字用火星的进制

“tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou”

然后就是例子:

29

29/13 = 2 2是高位,用火星的进制 选 hel

余数是3 是地位.用地球的进制 选mar

就是hel mar

然后我们这里

注意的地方是

如果你输入的是0 那么结果应该是tret而不是zero.

还是英语不行.

测试点1就考的是这个.

我的思路:

① 建立一个Map表.一个是<int,string>

一个是<string,int> 然后把他们初始化一下.

② 然后就是按照对应的输出.

测试点1的考点:

就是输入为0 的时候,输出是tret.

我的代码如下:

#include<iostream>
#include<map>
#include<string>
#include<cstring>
using namespace std;
int main(void) {
	int N;
	cin >> N;
	map<int, string> Data;
	map<string, int> CopyData;
	string Mars[13] = {"tret","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou" };
	string Earth[13] = {"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec" };
	for (int i = 0; i < 169; i++) {
		if (i / 13 < 1) 
			Data[i] = Earth[i % 13];
		else {
			Data[i] = Mars[i / 13];
			if(i%13!=0)		Data[i] = Data[i] + " " + Earth[i % 13];
		}
		CopyData[Data[i]] = i;
	}
	string a;
	cin.clear();	cin.ignore();		//清楚缓存
	for (int i = 0; i < N; ++i) {
		getline(cin, a);
		if (a[0] >= '0' && a[0] <= '9') 	cout << Data[atoi(a.c_str())];
		else		cout << CopyData[a];
		if (i != N - 1)	cout << "\n";
	}
	return 0;
}

下面和我一起欣赏柳神的代码:

柳神的思路:

分析:因为给出的可能是数字(地球文)也有可能是字母(火星文),所以用字符串s保存每一次的输入,因为如果是火星文则会出现空格,所以用getline接收一行的输入~计算string s的长度len,判断s[0]是否是数字,如果是数字,表示是地球文,则需要转为火星文,执行func1();如果不是数字,则说明是火星文,需要转为地球文,执行func2();

func1(int t)中,传入的值是string转int后的结果stoi(s),因为数字最大不超过168,所以最多只会输出两位火星文,如果t / 13不等于0,说明有高位,所以输出b[t/13];如果输出了高位(t/13不等于0)并且t % 13不等于0,说明有高位且有低位,所以此时输出空格;如果t % 13不等于0,说明有低位,此时输出a[t % 13];注意,还有个数字0没有考虑,因为数字0取余13等于0,但是要特别输出tret,所以在func1的最后一句判断中加一句t == 0,并将a[0]位赋值成tret即可解决0的问题~

func2()中,t1和t2一开始都赋值0,s1和s2用来分离火星文单词,因为火星文字符串只可能一个单词或者两个单词,而且一个单词不会超过4,所以先将一个单词的赋值给s1,即s1 = s.substr(0, 3);如果len > 4,就将剩下的一个单词赋值给s2,即s2 = s.substr(4, 3);然后下标j从1到12遍历a和b两个数组,如果a数组中有和s1或者s2相等的,说明低位等于j,则将j赋值给t2;如果b数组中有和s1相等的(b数组不会和s2相等,因为如果有两个单词,s2只可能是低位),说明高位有值,将j赋值给t1,最后输出t1 * 13 + t2即可~

代码如下:

#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"};
string s;
int len;
void func1(int t) {
    if (t / 13) cout << b[t / 13];
    if ((t / 13) && (t % 13)) cout << " ";
    if (t % 13 || t == 0) cout << a[t % 13];
}
void func2() {
    int t1 = 0, t2 = 0;
    string s1 = s.substr(0, 3), s2;
    if (len > 4) s2 = s.substr(4, 3);
    for (int j = 1; j <= 12; j++) {
        if (s1 == a[j] || s2 == a[j]) t2 = j;
        if (s1 == b[j]) t1 = j;
    }
    cout << t1 * 13 + t2;
}
int main() {
    int n;
    cin >> n;
    getchar();
    for (int i = 0; i < n; i++) {
        getline(cin, s);
        len = s.length();
        if (s[0] >= '0' && s[0] <= '9')
            func1(stoi(s));
        else
            func2();
        cout << endl;
    }
    return 0;
}

如果这篇文章对你有张帮助的话,可以用你高贵的小手给我点一个免费的赞吗

相信我,你也能变成光.

在这里插入图片描述

如果你有任何建议,或者是发现了我的错误,欢迎评论留言指出.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值