A1100 Mars Numbers (字符串处理、数字进制转换)

原题:https://pintia.cn/problem-sets/994805342720868352/problems/994805367156883456

这道题我学会了字符串按某个符号分割,输入整行到字符串等等,知识点详见
字符串分割:https://blog.csdn.net/qq_44761480/article/details/100542089
整行输入:https://blog.csdn.net/qq_44761480/article/details/98882802

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include<stack>
#include<queue>
#include<sstream>
using namespace std;

/*
A1100 Mars Numbers (20 分)
*/

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

//将火星数转换成地球数, 输出
void MtoE(string str) {

	istringstream in(str);
	string sa, sb;
	getline(in, sb, ' ');
	getline(in, sa, ' ');
	int a1 = 0, a2 = 0;		//十位,个位
	for (int i = 0; i < 13; i++) {
		if (sb == b[i])	//前一位
			a1 = i;
		if (sa == a[i] || sb == a[i])	//加上该数小于13
			a2 = i;
	}
	cout << a1 * 13 + a2;

}
//将地球数转换成火星数,输出
void EtoM(string str) {
	int num = stoi(str);

	if (num == 0)
	{
		cout << a[0];
		return;
	}
	if (num / 13 != 0)
		cout << b[num / 13] ;
	if (num / 13 != 0 && num % 13 != 0)
		cout << " ";
	if (num % 13 != 0)
		cout << a[num % 13];
}

int main() {
	int N;
	cin >> N;
	getchar();		//清除缓冲区的'\n'

	for (int i = 0; i < N; i++) {
		string str;
		getline(cin, str);
		if (str[0] >= '0' && str[0] <= '9') {
			EtoM(str);
		}
		else
			MtoE(str);
		cout << endl;
	}

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值