PTA——1015 德才论、1018 锤子剪刀布

1015 德才论

在这里插入图片描述

在这里插入图片描述

解决代码:

遇到问题:error: C++ requires a type specifier for all declarations,原来是结构体的成员函数忘记带上返回值类型了,C++还是严谨。
这道题算法全在cmp()函数上,如果只能用C语言那就麻烦太多了。

#include<bits/stdc++.h>
using namespace std;
struct people{
	string num;
	int pol,cap;
	int tot;
	void count(){
		this->tot=this->pol+this->cap;
	}
};
people name[100005];
vector<people> one,two,three,four;
bool cmp(people a,people b){
	if(a.tot>b.tot){
		return true;
	}else if(a.tot==b.tot&&a.pol>b.pol){
		return true;
	}else if(a.tot==b.tot&&a.pol==b.pol){
		return a.num<b.num;
	}else return false;
}
int main()
{
	int n,low,high,sum=0;;
	cin>>n>>low>>high;
	for(int i=0;i<n;i++){
		string num;
		int pol,cap;
		cin>>num>>pol>>cap;
		name[i].num=num;
		name[i].pol=pol;
		name[i].cap=cap;
		if(pol<low||cap<low) continue;
		sum++;
		name[i].count();
		if(pol>=high&&cap>=high) one.push_back(name[i]);
		else if(pol>=high&&cap<high) two.push_back(name[i]);
		else if(pol>=cap) three.push_back(name[i]);
		else four.push_back(name[i]);
	}
	cout<<sum<<endl;
	sort(one.begin(),one.end(),cmp);
	sort(two.begin(),two.end(),cmp);
	sort(three.begin(),three.end(),cmp);
	sort(four.begin(),four.end(),cmp);
	for(int i=0;i<one.size();i++){
		cout<<one[i].num<<' '<<one[i].pol<<' '<<one[i].cap<<endl;
	}
	for(int i=0;i<two.size();i++){
		cout<<two[i].num<<' '<<two[i].pol<<' '<<two[i].cap<<endl;
	}
	for(int i=0;i<three.size();i++){
		cout<<three[i].num<<' '<<three[i].pol<<' '<<three[i].cap<<endl;
	}
	for(int i=0;i<four.size();i++){
		cout<<four[i].num<<' '<<four[i].pol<<' '<<four[i].cap<<endl;
	}
	return 0;
} 

1018 锤子剪刀布

在这里插入图片描述
在这里插入图片描述

解决代码

这道题思路是很简单的,做一个哈希映射结果就出来了,如果口述的话很简单。
但是实战操作如果要用泛型容器map,自动排序是不稳定的排序,所以自己写一个哈希表,一个哈希映射函数会比较方便。

#include<bits/stdc++.h>
using namespace std;
int charToNum(char c){
	if(c=='B') return 0;
	if(c=='C') return 1;
	if(c=='J') return 2; 
} 
char numToChar(int n){
	if(n==0) return 'B';
	if(n==1) return 'C';
	if(n==2) return 'J';
}
int x[3],y[3];
int hash_(int x[]){
	int index=0,cmp=-1;
	for(int i=0;i<3;i++){
		if(x[i]>cmp){
			index=i;
			cmp=x[i];
		}
	}
	return index;
}
int main()
{
	int n,win=0,draw=0;
	cin>>n;
	for(int i=0;i<n;i++){
		char a,b;
		cin>>a;
		getchar();
		cin>>b;
		if(a==b) draw++;
		if((a=='C'&&b=='J')||(a=='J'&&b=='B')||(a=='B'&&b=='C')){
			win++;
			x[charToNum(a)]++;
		}else{
			y[charToNum(b)]++;
		}
	}
	cout<<win<<' '<<draw<<' '<<n-win-draw<<endl;
	cout<<n-win-draw<<' '<<draw<<' '<<win<<endl;
	cout<<numToChar(hash_(x))<<' '<<numToChar(hash_(y));
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

新西兰做的饭

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

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

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

打赏作者

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

抵扣说明:

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

余额充值