PAT B 1044 (C ++) - Long brush brother's title path

1044 Mars numbers (20 points)
Mars is 13 hexadecimal counting:
Earth 0 is called Martian tret.
Digital Earth Martian 1-12 were: jan, feb, mar, apr , may, jun, jly, aug, sep, oct, nov, dec.
Martian would later carry 12 high figures were called: tam, hel, maa, huh , tou, kes, hei, elo, syy, lok, mer, jou.
For example, the earth's digital translated into 29 Martian is hel mar; and Martian elo nov 115 correspond Digital Earth. In order to facilitate the exchange, please write programs to translate between the Earth and Mars numbers.

Input format:
given a positive input of the first line integer N (<100), followed by N rows, each row gives a [0, 169) within the digital section - Earth or text, the text or Mars.

Output format:
corresponding to each row of the input digital another language translation outputting in a row.

Sample input:
. 4
29
. 5
Elo-Nov
TAM

Output Sample:
HEL-Mar
On May
115
13 is

Problem-solving ideas: casual working according to the required output

#include<iostream>
#include<cstdio>
#include<string>
#include<map>
#include<cmath>
using namespace std;
const string ge[12] = { "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec" };
const string shi[12] = { "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou" };
int strToInt(string str){
	int sum = 0;
	for (int i = 0; i < str.length(); i++){
		int x = str[i] - '0';
		sum += (int)(x*pow(10, str.length()-i-1));
	}
	return sum;
}
int marsToEarth(string x){
	int index = x.find(" ");
	int sum = 0;
	if (index == x.npos){
		for (int i = 0; i < 12; i++){
			if (x == shi[i]){
				sum += (i + 1) * 13;
				return sum;
			}
		}
		for (int i = 0; i < 12; i++){
			if (x == ge[i]){
				sum += i + 1;
				return sum;
			}
		}
		return sum;
	}
	else{
		string x1 = x.substr(0, index);
		string x2 = x.substr(index + 1);
		for (int i = 0; i < 12; i++){
			if (x1 == shi[i]){
				sum += (i + 1) * 13;
				break;
			}
		}
		for (int i = 0; i < 12; i++){
			if (x2 == ge[i]){
				sum += i + 1;
				break;
			}
		}
		return sum;
	}
}
string earthToMars(int x){
	string result;
	int gewei = x % 13;
	int shiwei = x / 13;
	if (shiwei > 0){
		result += shi[shiwei-1];
		if (gewei > 0){
			result += " ";
			result += ge[gewei - 1];
			return result;
		}
		return result;
	}
	else{
		if (gewei > 0){
			result += ge[gewei - 1];
			return result;
		}
		else{
			return "tret";
		}
	}
}
int main(){
	string x;
	int num = 0;
	cin >> num;
	getchar();
	for (int i = 0; i < num; i++){
		getline(cin, x);
			int y = 0;
			if (x[0] >= '0' && x[0] <= '9'){
				y = strToInt(x);
				cout << earthToMars(y) << endl;
			}
			else{
				cout << marsToEarth(x) << endl;
			}
		}
	return 0;
}
Published 46 original articles · won praise 0 · Views 575

Guess you like

Origin blog.csdn.net/qq_23079139/article/details/104102614