PAT 1100 Mars Numbers (20)

本文介绍了一个程序设计问题,即实现地球与火星两种不同进制计数系统的相互转换。火星计数系统采用13进制,并使用特定词汇表示数字。程序需要能够处理来自两个星球的数字输入,并正确输出另一种形式的数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

思路:用string数组来保存火星数, 用map保存火星数对应的阿拉伯数字;
当输入时火星数,判断是有还有第二个输入的办法:如果输入的第一个字符串是0-12对应的火星数,则肯定没有第二个字符串输入;
若不满足上以条件, 则再吸收一个字符,判断是否是'\n', 若是则表示输入结束, 反之则表示还有一个字符串
当输入的是阿拉伯数字的时候, 只需要判断第一位是否是0-9的字符就能判断, 字符串的长度,由'\0'出现的位置决定
注意点:单独处理输入数字为0的情况
 1 #include<iostream>
 2 #include<vector>
 3 #include<map>
 4 using namespace std;
 5 string ch1[]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
 6 string ch2[]={"","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
 7 map<string, int> m1, m2;
 8 
 9 int main(){
10   int n, i;
11   cin>>n;
12   for(i=0; i<13; i++) m1[ch1[i]] = i;
13   for(i=0; i<13; i++) m2[ch2[i]] = i*13;
14   for(i=0; i<n; i++){
15     string a, b;
16     cin>>a;
17     if(a[0]<='9' && a[0]>='0'){
18       int temp=0, cnt=0;
19       while(a[cnt]!='\0') temp = temp*10 + (a[cnt++]-'0');
20       if(temp/13) {
21         cout<<ch2[temp/13];
22         if(temp%13) printf(" ");
23         else cout<<endl;
24       }
25       if(temp%13) cout<<ch1[temp%13]<<endl;
26       if(temp==0) cout<<ch1[0]<<endl;
27     }else{
28       int sum=0;
29       if(m2.count(a)){
30         sum += m2[a];
31         if(cin.get()!='\n') cin>>b;
32         sum += m1[b];;
33       }else sum += m1[a];
34       cout<<sum<<endl;
35     }
36   }
37   return 0;
38 }

 

转载于:https://www.cnblogs.com/mr-stn/p/9232117.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值