1100 Mars Numbers (20分)

题目链接1100 Mars Numbers

题意:

相当于进制转换,给出0-12和13的倍数对应的火星文,即tam对应13,hel对应26,等等依次对应。pat不能用gets();

#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
const int maxn = 20;
using namespace std;
void toMars(char s[]);
void toDigit(char s[]);
string low[13] = {"tret", "jan", "feb", "mar", "apr", "may",
				"jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string up[13] = {"", "tam", "hel", "maa", "huh", "tou", "kes",
				 "hei", "elo", "syy", "lok", "mer", "jou"};
			
int main(int argc, char** argv) {
	
	int n;
	scanf("%d", &n);
	getchar();
	char str[maxn];
	string t;
	while(n--) {
		int i = 0;
		while(scanf("%c", str[i++]) && (str[i - 1] != '\n' || str[i - 1] != EOF));
		str[i] = '\0';
		if (str[0] >= '0' && str[0] <= '9') 
			toMars(str);
		else
			toDigit(str);
	}
	return 0;
}
void toMars(char s[]) {
	int sum = 0;
	for (int i = 0; i < strlen(s); i++) {
		sum += (s[i] - '0') * 10;
	}
	int h = sum / 13;
	int mod = sum % 13;
	string ans = h == 0 ? low[mod] : up[h] + " " + low[mod];
	printf("%s\n", ans.c_str());
}
void toDigit(char s[]) {
	string str(s);
	int index = str.find(' ');
	string s1 = str.substr(0, index);
	string s2 = str.substr(index + 1, str.size() - index - 1);
	int sum = 0, i;
	if (s1.size() != 0) {
		for (i = 1; i < 13; i++)
			if (up[i].compare(s1) == 0) {
				sum += 13 * i;
				break;
			}
	}
	for (i = 0; i < 13; i++) {
		if (low[i].compare(s2) == 0) {
			sum += i;
			break;
		}
	}
	printf("%d\n", sum);
}



发布了17 篇原创文章 · 获赞 0 · 访问量 459

猜你喜欢

转载自blog.csdn.net/qq_37866436/article/details/104148401