PAT A1100 Mars Numbers

本文详细介绍了一种利用C++实现数字与火星文之间进制转换的方法,通过使用map和getline函数,有效地处理了不同类型的输入,实现了从地球数字到火星文及反向的转换。

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

题目描述

在这里插入图片描述

实现

码前思考

  1. 由于输入存在空格,所以需要一次性输入字符串,使用getline(cin,string);

代码实现

//注意getline的使用 
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
using namespace std;

map<int,string> mpLow;
map<int,string> mpHigh;
map<string,int> reMpLow;
map<string,int> reMpHigh;

string input;

void trans2Mars(){
	//将字母转成数字
	int number = stoi(input);
	
	//进行进制转换
	if(number >= 13){
		cout<<mpHigh[number/13];
		if(number%13 != 0){
			cout<<" "<<mpLow[number%13]<<endl; 			
		}else{
			cout<<endl;
		}
	}else{
		cout<<mpLow[number]<<endl;
	}
	 
}

void trans2Earth(){
	//首先看它的位数是否大于等于5位
	if(input.size() >= 5){
		int high = reMpHigh[input.substr(0,3)]*13;
		int low = reMpLow[input.substr(4,input.size()-4)];
		int res = high+low;
		printf("%d\n",res);
	}else{
		if(reMpLow.count(input)){
			int res = reMpLow[input];	
			printf("%d\n",res);
		}else{
			int res = reMpHigh[input]*13;	
			printf("%d\n",res);			
		}	
	} 
}

int main(){
	mpLow[0] = "tret";
	mpLow[1] = "jan";
	mpLow[2] = "feb";
	mpLow[3] = "mar";
	mpLow[4] = "apr";
	mpLow[5] = "may";
	mpLow[6] = "jun";
	mpLow[7] = "jly";
	mpLow[8] = "aug";
	mpLow[9] = "sep";
	mpLow[10] = "oct";
	mpLow[11] = "nov";
	mpLow[12] = "dec";
	mpHigh[1] = "tam";
	mpHigh[2] = "hel";
	mpHigh[3] = "maa";
	mpHigh[4] = "huh";
	mpHigh[5] = "tou";
	mpHigh[6] = "kes";
	mpHigh[7] = "hei";
	mpHigh[8] = "elo";
	mpHigh[9] = "syy";
	mpHigh[10] = "lok";
	mpHigh[11] = "mer";
	mpHigh[12] = "jou";
	for(map<int,string>::iterator it = mpLow.begin();it!=mpLow.end();it++){
		reMpLow[it->second] = it->first;
	}
	for(map<int,string>::iterator it = mpHigh.begin();it!=mpHigh.end();it++){
		reMpHigh[it->second] = it->first;
	}
	
	int N;
	scanf("%d\n",&N);
	while(N--){
		getline(cin,input);
		if(input[0] >= '0' && input[0] <= '9'){
			//表示是数字 
			trans2Mars(); 
		}else{
			//表示是字母 
			trans2Earth();
		} 
	}
	
	return 0;
} 

码后反思

  1. 这道题目我把它想复杂了,其实直接用数组hash就好了,没有必要使用map
  2. 柳神的代码如下:
    #include <iostream>
    #include <string>
    using namespace std;
    string a[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
    string b[13] = {"####", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
    string s;
    int len;
    void func1(int t) {
        if (t / 13) cout << b[t / 13];
        if ((t / 13) && (t % 13)) cout << " ";
        if (t % 13 || t == 0) cout << a[t % 13];
    }
    void func2() {
        int t1 = 0, t2 = 0;
        string s1 = s.substr(0, 3), s2;
        if (len > 4) s2 = s.substr(4, 3);
        for (int j = 1; j <= 12; j++) {
            if (s1 == a[j] || s2 == a[j]) t2 = j;
            if (s1 == b[j]) t1 = j;
        }
        cout << t1 * 13 + t2;
    }
    int main() {
        int n;
        cin >> n;
        getchar();
        for (int i = 0; i < n; i++) {
            getline(cin, s);
            len = s.length();
            if (s[0] >= '0' && s[0] <= '9')
                func1(stoi(s));
            else
                func2();
            cout << endl;
        }
        return 0;
    }
    
  3. 如果getline()函数之前还有换行没有吸收,一定要先吸收,不然getline()就会读取这个换行了。。。比如上面的代码中:
    scanf("%d\n",&N); // scanf("%d",&N);不对
    while(N--){
    	getline(cin,input);
    	if(input[0] >= '0' && input[0] <= '9'){
    		//表示是数字 
    		trans2Mars(); 
    	}else{
    		//表示是字母 
    		trans2Earth();
    	} 
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值