1100 Mars Numbers(stl (string字符串的处理))

1100 Mars Numbers (20 分)

People on Mars count their numbers with base 13:

  • Zero on Earth is called "tret" on Mars.
  • The numbers 1 to 12 on Earth 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

/**本题题意:

                 就是 将 0 - 12 用  "tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"
用这些字母表示, 将 13作为进位条件, 其余12个进位数用分别字母  "tam", "hel", "maa", "huh", "tou", "kes", "hei",
                 "elo", "syy", "lok", "mer", "jou" 表示, 现在给出 n组数据 有的数据是字母 有的数据是数字,字母需要转换为对应的数字,
                 数字需要转换为对应的字母.
    本题遇到的问题:
              1.对于字符串的处理 在输入第一个n时 有一个换行符  getline() 会吸收换行符,因此要用一个getchar()"吃掉"这个多余的换行符
              2.对于s[0]第一个字符的处理 首先的思路是将 0 - 9 放入一个数组中 通过for循环遍历判断是否存在这个字母,显然此种方法太过麻烦
                  直接判断ascall码 '0'< s[0] < '9' 就可以处理

              3.最好将局部变量写在一个位置, string类型的find函数 会返回第一个找到元素的下标, 如果没有找到会返回一个int类型数字npos,    
              4.代码过于冗余
    本题题解:
              1.输入的数据是 字母 和 数字的组合 此时用string 变量 s 来作输入, 通过 s[0] 是否 >= 0 || <=9(注意 闭区间 要 =) 判断是字母
                或是数字
              (将0 - 12(13个数字) (12个进位数(13的一次方, 13的二次方 等等 直到 13的 12次方))   一共有25个数字 设定一个string ss
              数组存放这 25个数字对应的字母
              2.当满足数字条件时(此时就要输出ss数组中对应的字母):
                                此时对输入的数字s 用stoi(s)进行转换 为int型 数字 用num变量接收, 通过 / 13得到 10位 用decade(英语的10位)变量接收
                                此时要得到(ss数组中进位位的下标索引还需要加上12个个位数字) 通过 %13 得到个位用 one变量接收
                                至于最后的输出格式 : 还需要判断当num变量为10位数时(刚好为次方上的数字 个位数的0是不会输出的)
              3. 对与输入的是字母 ( 此时就要输出ss数组中对应字母的数字):
                    对于字母的输入(题意中输入多行数据, 一行数据可能有2个字母,此时如果需要拿到一行中的两个字母, 就要用到 getline函数(cin,s))
                  (不知道数据的长度,效率不是很快) (cin.getline()适用于已经知道字符的长度)获取一行的字母, substr(下标开始的索引,截取元素的个数)
                  获得一行字母中的单个单词(题中的单词都是3个字符组成),最后进行字母 转换为 数字时,只需要用for循环找到 ss中 与 s 单词相同的索引并输出
                  就行了
                     
**/


具体代码:

#include<iostream>
#include<string>
using namespace std;
string ss[25] ={ "tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep",
				 "oct", "nov", "dec", "tam", "hel", "maa", "huh", "tou", "kes", "hei", 
				 "elo", "syy", "lok", "mer", "jou"};
bool isNum;
int main(){
	int n;
	string s;
	scanf("%d", &n);
	getchar();//吃掉输入n时按下的回车 
	while(n--){
		getline(cin, s);
		if(s[0] >= '0' && s[0] <= '9')
			isNum = true;
		if(isNum == true){ //此时是数字转换为火星文 
			int num = stoi(s);
			int decade = num / 13;
			int ones = num % 13;
			if(num < 13)
				printf("%s\n", ss[ones].c_str());
			else
				{
					if(ones != 0)
						printf("%s %s\n", ss[decade + 12].c_str(), ss[ones].c_str());
					else
						printf("%s\n", ss[decade + 12].c_str());
				}
		}else{ //将火星文转换为数字
			int a = 0, b = 0; 
			string letter1 = s.substr(0, 3), letter2;
			if(s.size() > 4)
			{
				letter2 = s.substr(4, 3);
			}
			for(int i = 0; i < 25; i++){
				if(i < 13 && (letter1 == ss[i] || letter2 == ss[i]))
					a = i;
				else if( i >= 13 && letter1 == ss[i])	
					b = i - 12;
			}
			printf("%d\n", b * 13 + a);
				
//				for(int i = 0; i < 25; i++){
//					if(letter2 == ss[i])
//						a = i;
//					if(letter1 == ss[i]){
//						b = i - 12;
//						break;
//					}
//				}
//				printf("%d\n", b * 13 + a);
//			}else{
//				for(int i = 0; i < 25; i++){
//					if(i < 13 && letter1 == ss[i]){
//						printf("%d\n", i);
//						break;
//					}else if(letter1 == ss[i]){
//						printf("%d\n", (i - 12) * 13);
//						break;
//					}
//				}	
//			}
		}
		isNum = false;
	}
	
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值