【PAT甲级】 1100 Mars Numbers 数字、字符及进制转换

【题意】

输入数字,将其转换为13位进制数,并根据要求的字符单位输出;输入字符,根据规则转换回10进制数字。

【原题链接】

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

【题解】

该题自己初次整的时候没能AC,先mark下大佬的思路,回头自己再试~

思路一:

思路一来自柳神,通过设置两个处理函数,分别处理数字输入与英文字符输入的情况;对于俩个单词的英文字符串处理,柳神用的是先getline()读入,然后用cstring下的.substr()提取,然后遍历判别处理。

#include <iostream>
#include <cstring>
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];  //注意0要处理输出tret
}

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();  //getline()前吸收回车
	for(int i=0;i<n;i++){
	    getline(cin,s);
	    len=s.length();  //存下位数,为英文字符处理(提取)准备
	    if(s[0]<='9')  //根据头个字符选择处理方式
	        func1(stoi(s));
	    else{
	        func2();
	    }
	    cout<<endl;
	}
	
	return 0;
}

思路二:

思路二来自acwing的y总,通过stringstream流来辅助处理英文字符输入的处理。咱头回见这个语法,stackflow上一个回答说和iostream差不多,然后它可以当作一个虚拟的I/O来用,就是可以往里边输入,也可以读出的样子~

#include <iostream>
#include <cstring>
#include <sstream> 
using namespace std;

char a[25][5]={
    "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"
};

int get(string s){
    int res=0;
    for(int i=0;i<25;i++){  //遍历a容器,来确定该string-火星数字(字母),为哪个具体数值
        if(a[i]==s){
            if(i>12) res=(i-12)*13;
            else res=i;
            return res;
        }
    }
}

int main(){
	int n;
	cin>>n;
	getchar();
	
	while(n--){
	    string line;
	    getline(cin,line);
	    
	    stringstream ssin(line);  //输入string流,从line里
	    if(line[0]<='9'){
	        int v;
	        ssin>>v;  //从流 输出给int v
	        if(v<13) cout<<a[v]<<endl;  //单个低位情况,直接输出
	        else{
	            if(v%13==0) cout<<a[v/13+12]<<endl;  //单个高位火星数字 情况
	            else{
	                cout<<a[v/13+12]<<' '<<a[v%13]<<endl;  //先高位,再低位输出
	            }
	        }
	    }else{  //读入火星数字(字母)情况处理
	        int res=0;
	        string word;
	        while(ssin>>word) res+=get(word); 从流 输出给string word,用函数转回数值;
	        cout<<res<<endl;
	    }
	}
	
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值