结构体与STL入门

这些C++代码片段是为解决不同类型的竞赛问题编写的,包括寻找歌唱比赛中中间分数、寻找旗鼓相当的对手、确定最厉害的学生、处理不重复数字以及模拟演唱会和BAN-PICK策略。它们涉及排序算法和数据结构的应用。
摘要由CSDN通过智能技术生成

A - 歌唱比赛

 

#include<bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[])
{
	int n,i,j,m;
	float max=-1;
	scanf("%d%d",&n,&m);
	for(i = 0 ; i<n ; i++){
		int a[20];
		float sum=0;
		for(j=0 ; j<m ; j++){
			scanf("%d",&a[j]);
		}
		sort(a,a+m);
		for(j=1 ; j<m-1 ; j++){
			sum=sum+a[j];
		}
		if(sum/(m*1.0-2)>max){
			max=sum/(m*1.0-2);
		}
	}
	printf("%.2f",max);
	return 0;
}

B - 旗鼓相当的对手 - 加强版

 

#include<bits/stdc++.h>
using namespace std;
struct stu
{	
	char a[10];
    int b;
    int c;
    int d;
};
int main()
{
	int n;
	struct  stu s1[1000];
	scanf("%d",&n);
	for(int i =0 ; i<n ; i++){
		scanf("%s %d %d %d",&s1[i].a,&s1[i].b,&s1[i].c,&s1[i].d);
	}
	for(int i =0 ; i<n ; i++){
		for(int j =  i+1; j<n ; j++){
			if(fabs(s1[i].b-s1[j].b)<=5&&fabs(s1[i].c-s1[j].c)<=5&&fabs(s1[i].d-s1[j].d)<=5&&fabs(s1[i].b+s1[i].c+s1[i].d-(s1[j].b+s1[j].c+s1[j].d))<=10){
					printf("%s %s\n",s1[i].a,s1[j].a);		
			} 
		}
	}
	return 0;
}

C - 最厉害的学生

#include<bits/stdc++.h>
using namespace std;
struct stu
{	
	char a[10];
    int b;
    int c;
    int d;
    int s;
};
int main()
{
	int n;
	struct  stu s1[1000];
	scanf("%d",&n);
	for(int i =0 ; i<n ; i++){
		scanf("%s %d %d %d",&s1[i].a,&s1[i].b,&s1[i].c,&s1[i].d);
		s1[i].s=s1[i].b+s1[i].c+s1[i].d;
	}
	int max=0,m=-1;
		for(int j = 0; j<n ; j++){
			if(s1[j].s>m){
					m=s1[j].s;
					max=j;
			} 

		}
		printf("%s %d %d %d",s1[max].a,s1[max].b,s1[max].c,s1[max].d);
	return 0;
}

D - 不重复数字

  

#include<bits/stdc++.h>
using namespace std;
struct num{
	int id;
	long long z;
}s[50024];
int read(){
    int w=1,q=0;
	char ch=' ';
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')w=-1,ch=getchar();
    while(ch>='0'&&ch<='9')q=q*10+ch-'0',ch=getchar();
    return w*q;
}
bool cmz(struct num a,struct num b){
	if(a.z>b.z)
		return true;
	if(a.z==b.z)
		if(a.id<b.id)
			return true;
	return false;
}
bool cmid(struct num a,struct num b){
	if(a.id<b.id)
		return true;
	return false;
}
signed main(void){
	int t,n,pd;
	t = read();
	while(t--){
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			s[i].id = i;
			s[i].z = read();
		}
		sort(s+1,s+n+1,cmz);
		for(int i=1;i<=n;i+=pd){
			pd = 1;
			for(int j=i+1;j<=n;j++){
				if(s[i].z==s[j].z)s[j].z = -2147483659,pd++;
				else break;
			}
		}
		sort(s+1,s+n+1,cmid);
		for(int i=1;i<=n;i++){
			if(s[i].z!=-2147483659)printf("%lld ",s[i].z);
		}
		printf("\n");
	}
	return 0;
}

 

G - 演唱会

#include<bits/stdc++.h>
using namespace std;
#define int long long
struct node
{
    int num;
    int h=0;
} p[100005];
bool cmp(node x,node y){
    return x.h > y.h;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m, a, b;
    cin >> n >> m >> a >> b;
    int mx = 0, bj;
    for (int i = 1; i <= n;i++)
    {
        p[i].num = i;
    }
    for (int i = 1; i <= a;i++){
        for (int j = 1; j <= n;j++){
            int x;
            cin >> x;
            if(i==b) {
                if(x>mx)
                    mx = x, bj = j;
            }
            p[j].h += x;
        }  
    }
    int k = p[bj].h;
    sort(p + 1, p + n + 1, cmp);
    if(p[m].h>k){
        for (int i = 1; i < m;i++){
            cout << p[i].num<<" ";
        }
        cout << bj;
    }
    else {
        cout << bj;
        for (int i = 1; i <= m;i++){
            if(p[i].num!=bj) cout <<" "<< p[i].num;
        }
    }
}

 
H - BAN-PICK

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=100000;
struct stu
{	
	string a;
    char b;
    ll c;
};	
struct  stu s1[N];
	struct  stu s2[N];
bool cmp1(stu x,stu y){
    return x.c > y.c;
}
bool cmp2(stu x,stu y){
    return x.c > y.c;
}
int main()
{
	ll n,m;
	cin>>n>>m;

	ll c1=0,c2=0;
	for(int i=0 ; i<n+m ; i++){
		string x;
		char y;
		ll z;
		cin>>x>>y>>z;
		if(y=='S'){
			s1[c1].a=x;
			s1[c1].c=z;
			c1++;
		}
		if(y=='H'){
			s2[c2].a=x;
			s2[c2].c=z;
			c2++;
		}
	}
	sort(s1,s1+c1,cmp1);
	sort(s2,s2+c2,cmp2);
	cout<<s2[2].a<<endl;
	for(int i =  5; i<9 ; i++)
		cout<<s1[i].a<<endl;
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值