PTA——1041 考试座位号、1042 字符统计、1043 输出PATest、1044 火星数字

1041 考试座位号

在这里插入图片描述

解决代码

利用哈希表整合信息输出即可。

#include<iostream>
#include<vector>
#include<algorithm>
#include<unordered_map>
using namespace std;
vector<int> vec;
struct stu{
	string s;
	int x,y;
};
stu s[1005];
unordered_map<int,stu> map_;
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>s[i].s>>s[i].x>>s[i].y;
		map_[s[i].x]=s[i];
	}
	int m;
	cin>>m;
	vec.resize(m);
	for(int i=0;i<m;i++){
		cin>>vec[i];
	}
	for(int i=0;i<m;i++){
		cout<<map_[vec[i]].s<<' '<<map_[vec[i]].y<<endl;
	}
	return 0;
}

1042 字符统计

在这里插入图片描述

解决代码

利用哈希表,最后输出字母即可。

#include<iostream>
#include<vector>
#include<algorithm>
#include<unordered_map>
using namespace std;
vector<int> vec(200,0);
bool pan(char c){
	if(c>='a'&&c<='z'||c>='A'&&c<='Z') return true;
	else return false;
}
int main(){
	string str;
	getline(cin,str);
	for(auto i:str){
		if(i>='A'&&i<='Z') i=i-'A'+'a';
		vec[i]++;
	}
	int max_=0;
	char c;
	for(int i=0;i<200;i++){
		if(pan((char)i)&&vec[i]>max_){
			max_=vec[i];
			c=(char)i;
		}
	}
	cout<<c<<' '<<max_<<endl;
	return 0;
}

1043 输出PATest

在这里插入图片描述

解决代码

利用哈希表,最后按照PATest顺序输出,只要这几个字符的哈希值不为0,就循环输出,谁先到0了谁停止输出。

#include<iostream>
#include<vector>
#include<algorithm>
#include<unordered_map>
using namespace std;
int hash_[128];
int main(){
	string str;
	cin>>str;
	for(auto i:str) hash_[i]++;
	while(hash_['P']>0||hash_['A']>0||hash_['T']>0||hash_['e']>0||hash_['s']>0||hash_['t']>0){
		if(hash_['P']-->0) cout<<'P';
		if(hash_['A']-->0) cout<<'A';
		if(hash_['T']-->0) cout<<'T';
		if(hash_['e']-->0) cout<<'e';
		if(hash_['s']-->0) cout<<'s';
		if(hash_['t']-->0) cout<<'t';
	}
	return 0;
}

1044 火星数字

在这里插入图片描述

解决代码

处理起来有点麻烦,注意进制转换的经典例子:130转换为13进制,应该为oct而没有后面的0(tret)。

#include<iostream>
#include<vector>
#include<algorithm>
#include<unordered_map>
using namespace std;
string low[]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
string high[]={"tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
unordered_map<string,int> mapx,mapd;
void init(){
	for(int i=0;i<13;i++){
		mapx[low[i]]=i;
	}
	for(int i=1;i<=12;i++){
		mapd[high[i-1]]=i;
	}
}
int main(){
	init();
	int n;
	cin>>n;
	getchar();
	while(n--){
		string str;
		getline(cin,str);
		int num=0;
		if(str[0]>='0'&&str[0]<='9'){
			num=stoi(str);
			int h=num/13;
			if(h!=0){
				cout<<high[--h];
				if(num%13!=0)  cout<<' '<<low[num%13]<<endl;
				else cout<<endl;
			}else{
				cout<<low[num%13]<<endl;
			}
		}
		else{
			int sum=0;
			if(str.size()>3){
				string a=str.substr(0,3);
				sum+=mapd[a]*13;
				str=str.substr(4,3);
			}
			if(mapx[str]==0) sum+=mapd[str]*13;
			else sum+=mapx[str];
			cout<<sum<<endl;
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

新西兰做的饭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值