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;
}