PAT Basic 1044

原文链接: http://www.cnblogs.com/yxp400/p/9460389.html
1044 火星数字

火星人是以 13 进制计数的:

  • 地球人的 0 被火星人称为 tret。
  • 地球人数字 1 到 12 的火星文分别为:jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec。
  • 火星人将进位以后的 12 个高位数字分别称为:tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou。

例如地球人的数字 29 翻译成火星文就是 hel mar;而火星文 elo nov 对应地球数字 115。为了方便交流,请你编写程序实现地球和火星数字之间的互译。

输入格式:

输入第一行给出一个正整数 N(<100),随后 N 行,每行给出一个 [0, 169) 区间内的数字 —— 或者是地球文,或者是火星文。

输出格式:

对应输入的每一行,在一行中输出翻译后的另一种语言的数字。

输入样例:

4
29
5 
elo nov
tam  

输出样例:

hel mar
may
115
13
  题解:这道题并不难,但是坑点挺多的,比如说他可能直接输出一个高位的火星数字,这时候不需要输出低位的火星数字等等,详见代码。

代码如下:

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

转载于:https://www.cnblogs.com/yxp400/p/9460389.html

猜你喜欢

转载自blog.csdn.net/weixin_30687051/article/details/94803555