码迷,mamicode.com
首页 > 其他好文 > 详细

PAT 1100

时间:2015-12-25 11:50:15      阅读:364      评论:0      收藏:0      [点我收藏+]

标签:

1100. Mars Numbers (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

People on Mars count their numbers with base 13:

  • Zero on Earth is called "tret" on Mars.
  • The numbers 1 to 12 on Earch is called "jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec" on Mars, respectively.
  • For the next higher digit, Mars people name the 12 numbers as "tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou", respectively.

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
tam
Sample 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;
}

 

PAT 1100

标签:

原文地址:http://www.cnblogs.com/RookieCoder/p/5075288.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!