PAT B1044 火星数字 (20分)

题目链接https://pintia.cn/problem-sets/994805260223102976/problems/994805279328157696

题目描述
火星人是以 13 进制计数的:

地球人的 0 被火星人称为 tret。
地球人数字 1 到 12 的火星文分别为:jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec。
火星人将进位以后的 12 个高位数字分别称为:tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou。
例如地球人的数字 29 翻译成火星文就是 hel mar;而火星文 elo nov 对应地球数字 115。为了方便交流,请你编写程序实现地球和火星数字之间的互译。

输入
输入第一行给出一个正整数 N(<100),随后 N 行,每行给出一个 [0, 169) 区间内的数字 —— 或者是地球文,或者是火星文。

输出
对应输入的每一行,在一行中输出翻译后的另一种语言的数字。

样例输入
4
29
5
elo nov
tam

样例输出
hel mar
may
115
13

代码

我的代码。。。

#include <iostream>
#include <cstdio>
#include <string>
#include <map>
#include <algorithm>

using namespace std;

int main() {
	map<string, int> mp1, mp2;
	mp1["tret"] = 0;
	mp1["jan"] = 1;
	mp1["feb"] = 2;
	mp1["mar"] = 3;
	mp1["apr"] = 4;
	mp1["may"] = 5;
	mp1["jun"] = 6;
	mp1["jly"] = 7;
	mp1["aug"] = 8;
	mp1["sep"] = 9;
	mp1["oct"] = 10;
	mp1["nov"] = 11;
	mp1["dec"] = 12;
	mp2["tam"] = 1;
	mp2["hel"] = 2;
	mp2["maa"] = 3;
	mp2["huh"] = 4;
	mp2["tou"] = 5;
	mp2["kes"] = 6;
	mp2["hei"] = 7;
	mp2["elo"] = 8;
	mp2["syy"] = 9;
	mp2["lok"] = 10;
	mp2["mer"] = 11;
	mp2["jou"] = 12;

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

	int n;
	string temp;
	scanf("%d", &n);	
	getchar();
	for(int i = 0; i < n; i++ ){		
		getline(cin, temp);
		int len = temp.size();
		if(temp[0] >= '0' && temp[0] <= '9') {
			int a = 0;
			for(int i = 0; i < len; i++)
				a = a * 10 + (temp[i] - '0');
			int d1 = a % 13;		
			int d2 = a / 13;		
			if(d1 ==0 && d2 == 0)
				cout<<str1[0];
			if(d2)
				cout<<str2[d2];
			if(d1 && d2)
				cout<<' ';
			if(d1)
				cout<<str1[d1];
			cout<<endl;
		} 
		else{		
			int i = 0, b1 = 0, b2 = 0;
			string s1, s2;
			if(len > 4) {		
				for(; temp[i] != ' '; i++)
					s2 += temp[i];
				b2 = mp2[s2];	
				i++;		
				for(; temp[i] != '\0'; i++)
					s1 += temp[i];
				b1 = max(mp1[s1], mp2[s1]);	
			}
			else{												
				for(; temp[i] != '\0'; i++)
					s1 += temp[i];
				if(mp1[s1] > mp2[s1])					
					b1 = max(mp1[s1], mp2[s1]);
				else											
					b2 = max(mp1[s1], mp2[s1]);
			}
			printf("%d\n", b2 * 13 + b1);
			s1.clear();
			s2.clear();
		}
		
	}
	return 0;
}

大佬的代码1:

#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;
}

大佬的代码2:

#include <map>
#include <cstring>
#include <iostream>
using namespace std;
string arrl[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string arru[13] = {"####", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
map<string,int> mapl = {{"tret",0}, {"jan",1}, {"feb",2}, {"mar",3}, {"apr",4}, {"may",5}, {"jun",6}, {"jly",7}, {"aug",8}, {"sep",9}, {"oct",10}, {"nov",11}, {"dec",12}};
map<string,int> mapu = {{"tam",1}, {"hel",2}, {"maa",3}, {"huh",4}, {"tou",5}, {"kes",6}, {"hei", 7}, {"elo",8}, {"syy",9}, {"lok", 10}, {"mer", 11}, {"jou",12}};

int main()
{
    int n;
    scanf("%d\n", &n);
    string s;
    for (int i = 0; i < n; i++)
    {
        getline(cin, s);
        if (s[0] >= '0' && s[0] <= '9')
        {
            int num = stoi(s);
            if(num<13) {
                printf("%s\n", arrl[num].data());
            } else if(num>=13 && (num%13==0)) {
                printf("%s\n",arru[num/13].data());
            } else {
                printf("%s %s\n", arru[num/13].data(), arrl[num%13].data());
            }
        }
        else
            printf("%d\n", s.length()==3 ? mapl[s]+mapu[s]*13 : mapu[s.substr(0,3)]*13+mapl[s.substr(4,3)]);
    }
    return 0;
}

【注】:

  1. 能被13整除的只输出高位火星文(一个case);
  2. 0转换成火星文输出 “tret”(一个case);
  3. map可以不用,参考柳神代码;
  4. map可以批量赋值,但vs2008不支持!
  5. stoi(s) 把数字字符串转换成int输出,头文件是 #include ,但是vs2010之前的没有这个函数,OJ可;
发布了328 篇原创文章 · 获赞 12 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Rhao999/article/details/105103794
今日推荐