标签:
People on Mars count their numbers with base 13:
For examples, the number 29 on Earth is called "hel mar" on Mars; and "elo nov" on Mars corresponds to 115 on Earth. In order to help communication between people from these two planets, you are supposed to write a program for mutual translation between Earth and Mars number systems.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (< 100). Then N lines follow, each contains a number in [0, 169), given either in the form of an Earth number, or that of Mars.
Output Specification:
For each number, print in a line the corresponding number in the other language.
Sample Input:4 29 5 elo nov tamSample Output:
hel mar may 115 13
解析:略
#include <iostream> #include <string> #include <cstring> #include <vector> #include <map> #include <stdio.h> using namespace std; string dict[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"}; string dict2[13] = {"", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"}; map<string, int> table; map<string, int> table2; void init(){ for(int i=0; i<13; i++){ table[dict[i]] = i; } for(int i=0; i<13; i++){ table2[dict2[i]] = i; } return; } int pow(int n, int e){ if(e == 0){ return 1; } else{ return n * pow(n, e-1); } } int mar2ear(string input){ vector<string> tmp; int len = input.length(); string str; for(int i=0; i<len; i++){ if(input[i] != ‘ ‘){ str.push_back(input[i]); if(i == len-1){ tmp.push_back(str); str.clear(); } } else{ tmp.push_back(str); str.clear(); } } int result = 0; for(int i=0; i<tmp.size(); i++){ if(table[tmp[i]]){ result += table[tmp[i]]; } else{ result += table2[tmp[i]] * 13; } } return result; } string ear2mar(int n){ string result; int tmp1 = n/13; int tmp2 = n%13; if(tmp1 == 0){ result = dict[tmp2]; } else{ result.append(dict2[tmp1]); if(tmp2 != 0){ result.append(" "); result.append(dict[tmp2]); } } return result; } int toint(string input){ int result = 0; int pos = 0; for(int i=input.length()-1; i>=0; i--){ result += (input[i] - ‘0‘) * pow(10, pos); pos++; } return result; } int main(){ init(); int n; scanf("%d", &n); getchar(); string line; while(n--){ getline(cin, line); //is digit if(line[0] >= ‘0‘ && line[0] <= ‘9‘){ cout<<ear2mar(toint(line))<<endl; } else{ cout<<mar2ear(line)<<endl; } } return 0; }
标签:
原文地址:http://www.cnblogs.com/RookieCoder/p/5075288.html