1044 火星数字

pat乙级 1044 火星数字


火星人是以 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) 区间内的数字 —— 或者是地球文,或者是火星文。

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

输入样例1:

4
29
5
elo nov
tam

输出样例1:

hel mar
may
115
13

坑点: 比如:10进制的13—>转换为13进制为----> 1 0
1 0 转换为火星文就是 tam ,而不是 tam tret 。
所以当10进制的数能整除13,转换为火星文低位不输出tret 。

if (num % 13 == 0){
		switch (num/13)
		{
		case 1:cout << "tam"<<endl; break;
		case 2:cout << "hel" << endl;  break;
		case 3:cout << "maa" << endl;  break;
		case 4:cout << "huh" << endl;  break;
		case 5:cout << "tou" << endl;  break;
		case 6:cout << "kes" << endl;  break;
		case 7:cout << "hei" << endl;  break;
		case 8:cout << "elo" << endl;  break;
		case 9:cout << "syy" << endl;  break;
		case 10:cout << "lok" << endl;  break;
		case 11:cout << "mer" << endl;  break;
		case 12:cout << "jou" << endl;  break;
		}
}

第一行输入完数量N后 cin>>N ,还要getchar()接收空格,不然下面getline()接收字符串的时候,第一个字符就会接收空格。

int n;
cin >> n;
getchar();    接收空格
vector<string> input;
for (int i = 0; i < n; i++){ 
	string inpu;
	getline(cin, inpu);
	input.push_back(inpu);
}

总结:
本质就是10进制与13进制的转换

AC代码:

#include<iostream>
#include<vector>
#include<string>
#include<stack>

using namespace std;

int main(){
	int n;
	cin >> n;
	getchar();
	vector<string> input;
	for (int i = 0; i < n; i++){ 
		string inpu;
		getline(cin, inpu);
		input.push_back(inpu);
	}
	
	for (int i = 0; i < n; i++){
		stack<int> s;
		if (input[i][0] >= '0'&&input[i][0] <= '9'){
			int num = stoi(input[i], 0, 10);
			if (num == 0){
				cout << "tret" << endl;
			}
			else
			{
				if (num % 13 == 0){
					switch (num/13)
					{
					case 1:cout << "tam"<<endl; break;
					case 2:cout << "hel" << endl;  break;
					case 3:cout << "maa" << endl;  break;
					case 4:cout << "huh" << endl;  break;
					case 5:cout << "tou" << endl;  break;
					case 6:cout << "kes" << endl;  break;
					case 7:cout << "hei" << endl;  break;
					case 8:cout << "elo" << endl;  break;
					case 9:cout << "syy" << endl;  break;
					case 10:cout << "lok" << endl;  break;
					case 11:cout << "mer" << endl;  break;
					case 12:cout << "jou" << endl;  break;
					}
				}
				else
				{
					while (num != 0)
					{
						s.push(num % 13);
						num /= 13;
					}
					if (s.size() == 2){
						switch (s.top())
						{
						case 1:cout << "tam "; s.pop(); break;
						case 2:cout << "hel "; s.pop(); break;
						case 3:cout << "maa "; s.pop(); break;
						case 4:cout << "huh "; s.pop(); break;
						case 5:cout << "tou "; s.pop(); break;
						case 6:cout << "kes "; s.pop(); break;
						case 7:cout << "hei "; s.pop(); break;
						case 8:cout << "elo "; s.pop(); break;
						case 9:cout << "syy "; s.pop(); break;
						case 10:cout << "lok "; s.pop(); break;
						case 11:cout << "mer "; s.pop(); break;
						case 12:cout << "jou "; s.pop(); break;
						}
						switch (s.top())
						{
					    case 0:cout << "tret"<<endl; s.pop(); break;
						case 1:cout << "jan" << endl; s.pop(); break;
						case 2:cout << "feb" << endl; s.pop(); break;
						case 3:cout << "mar" << endl; s.pop(); break;
						case 4:cout << "apr" << endl; s.pop(); break;
						case 5:cout << "may" << endl; s.pop(); break;
						case 6:cout << "jun" << endl; s.pop(); break;
						case 7:cout << "jly" << endl; s.pop(); break;
						case 8:cout << "aug" << endl; s.pop(); break;
						case 9:cout << "sep" << endl; s.pop(); break;
						case 10:cout << "oct" << endl; s.pop(); break;
						case 11:cout << "nov" << endl; s.pop(); break;
						case 12:cout << "dec" << endl; s.pop(); break;
						}
					}
					else if (s.size() == 1){
						switch (s.top())
						{
						case 0:cout << "tret" << endl; s.pop(); break;
						case 1:cout << "jan" << endl; s.pop(); break;
						case 2:cout << "feb" << endl; s.pop(); break;
						case 3:cout << "mar" << endl; s.pop(); break;
						case 4:cout << "apr" << endl; s.pop(); break;
						case 5:cout << "may" << endl; s.pop(); break;
						case 6:cout << "jun" << endl; s.pop(); break;
						case 7:cout << "jly" << endl; s.pop(); break;
						case 8:cout << "aug" << endl; s.pop(); break;
						case 9:cout << "sep" << endl; s.pop(); break;
						case 10:cout << "oct" << endl; s.pop(); break;
						case 11:cout << "nov" << endl; s.pop(); break;
						case 12:cout << "dec" << endl; s.pop(); break;
						}
					}
				}
				
			}		
		}
		else
		{
			if (input[i].size() > 3){
				string gao = input[i].substr(0, 3);
				string di = input[i].substr(4);
				int sum = 0;
				if (gao == "tam"){
					sum += 1 * 13;
				}
				else if (gao == "hel"){
					sum += 2 * 13;
				}
				else if (gao == "maa"){
					sum += 3 * 13;
				}
				else if (gao == "huh"){
					sum += 4 * 13;
				}
				else if (gao == "tou"){
					sum += 5 * 13;
				}
				else if (gao == "kes"){
					sum += 6 * 13;
				}
				else if (gao == "hei"){
					sum += 7 * 13;
				}
				else if (gao == "elo"){
					sum += 8 * 13;
				}
				else if (gao == "syy"){
					sum += 9 * 13;
				}
				else if (gao == "lok"){
					sum += 10 * 13;
				}
				else if (gao == "mer"){
					sum += 11 * 13;
				}
				else if (gao == "jou"){
					sum += 12 * 13;
				}

				if (di == "jan"){
					sum += 1;
				}
				else if (di == "feb"){
					sum += 2;
				}
				else if (di == "mar"){
					sum += 3;
				}
				else if (di == "apr"){
					sum += 4;
				}
				else if (di == "may"){
					sum += 5;
				}
				else if (di == "jun"){
					sum += 6;
				}
				else if (di == "jly"){
					sum += 7;
				}
				else if (di == "aug"){
					sum += 8;
				}
				else if (di == "sep"){
					sum += 9;
				}
				else if (di == "oct"){
					sum += 10;
				}
				else if (di == "nov"){
					sum += 11;
				}
				else if (di == "dec"){
					sum += 12;
				}
				else if (di == "tret"){
					sum += 0;
				}
				cout << sum << endl;
			}
			else
			{
				string di = input[i].substr(0);
				int sum = 0;
				if (di == "jan"){
					sum += 1;
				}
				else if (di == "feb"){
					sum += 2;
				}
				else if (di == "mar"){
					sum += 3;
				}
				else if (di == "apr"){
					sum += 4;
				}
				else if (di == "may"){
					sum += 5;
				}
				else if (di == "jun"){
					sum += 6;
				}
				else if (di == "jly"){
					sum += 7;
				}
				else if (di == "aug"){
					sum += 8;
				}
				else if (di == "sep"){
					sum += 9;
				}
				else if (di == "oct"){
					sum += 10;
				}
				else if (di == "nov"){
					sum += 11;
				}
				else if (di == "dec"){
					sum += 12;
				}
				else if (di == "tret"){
					sum += 0;
				}
				else if (di == "tam"){
					sum += 1*13;
				}
				else if (di == "hel"){
					sum += 2 * 13;
				}
				else if (di == "maa"){
					sum += 3 * 13;
				}
				else if (di == "huh"){
					sum += 4 * 13;
				}
				else if (di == "tou"){
					sum += 5 * 13;
				}
				else if (di == "kes"){
					sum += 6 * 13;
				}
				else if (di == "hei"){
					sum += 7 * 13;
				}
				else if (di == "elo"){
					sum += 8 * 13;
				}
				else if (di == "syy"){
					sum += 9 * 13;
				}
				else if (di == "lok"){
					sum += 10 * 13;
				}
				else if (di == "mer"){
					sum += 11 * 13;
				}
				else if (di == "jou"){
					sum += 12 * 13;
				}
				cout << sum << endl;
			}
		}
	}


	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值