PAT 乙级 1044

题目

    题目地址:PAT 乙级 1044

 

思路

    简单的进制转化问题,根据题意进行相应的进制转化即可,因为题目已经划定了数据的求解范围,甚至连进制转化中的循环都不需要,进行简单计算就可以得出结果;

    但本题还是有坑,结果就在这个坑上栽了很多次;10进制化为13进制的过程中,对于可以被13整除的数,后面的0需要省略,而不能打印出来,例如对于13、26这样的数,最终输出的结果是tam、hel,而不是tam tret、hel tret;

 

代码

 1 #include <iostream>
 2 #include <string>
 3 #include <cmath>
 4 using namespace std;
 5 
 6 const string gewei[] = { "tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec" };
 7 const string shiwei[] = { "###", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou" };
 8 
 9 void ten2tir(string str) {
10     int num = 0, D_val = 1;
11     for (int i = 0; i < str.size(); i++) {
12         num += (str[i] - 48) * pow(10, (str.size() - D_val));
13         D_val++;
14     }
15     if (num < 13)
16         cout << gewei[num] << endl;
17     else {
18         if (num % 13 == 0)
19             cout << shiwei[num / 13] << endl;
20         else
21             cout << shiwei[num / 13] << ' ' << gewei[num % 13] << endl;
22     }
23 }
24 
25 void tir2ten(string str) {
26     int loc_space = 0;
27     int sum = 0;
28     for (int i = 0; i < str.size(); i++)
29         if (str[i] == ' ')
30             loc_space = i;
31     if (loc_space) {
32         string tmp;
33         tmp = str.substr(0, loc_space);
34         for (int i = 1; i < 13; i++)
35             if (tmp == shiwei[i])
36                 sum += i * 13;
37         tmp = str.substr(loc_space + 1, str.size() - loc_space - 1);
38         for (int i = 0; i <= 13; i++)
39             if (tmp == gewei[i])
40                 sum += i;
41     }
42     else {
43         for (int i = 1; i < 13; i++)
44             if (str == shiwei[i])
45                 sum += i * 13;
46         for (int i = 0; i <= 13; i++)
47             if (str == gewei[i])
48                 sum += i;
49     }
50     cout << sum << endl;
51 }
52 
53 int main() {
54     int n = 0;
55     string str;
56     cin >> n;
57     cin.ignore();
58     for (int i = 0; i < n; i++) {
59         getline(cin, str);
60         if (isdigit(str[0]))
61             ten2tir(str);
62         else
63             tir2ten(str);
64     }
65 
66     return 0;
67 }

 

posted @ 2018-10-02 18:31  moujun  阅读(280)  评论(0编辑  收藏  举报