结构体 P5740 【深基7.例9】最厉害的学生+P5741 【深基7.例10】旗鼓相当的对手 - 加强版+天梯赛的猜数字

在这里插入图片描述

#include<iostream>
#include<algorithm>
using namespace std;
struct student
{
	char name[20];
	int chinese;
	int math;
	int english;
	int sum;
	int sequence;//顺序
};
bool cmp(student a, student b)
{
	if (a.sum != b.sum) return a.sum > b.sum;
	else  return a.sequence < b.sequence;//总分相同的时候输出先输入的
}
int main()
{
	student stu[1000];
	int n, i, j, k;
	cin >> n;
	for (i = 0; i < n; i++)
	{
		cin >> stu[i].name >> stu[i].chinese >> stu[i].math >> stu[i].english;
		stu[i].sum = stu[i].math + stu[i].chinese + stu[i].english;
		stu[i].sequence = i;
	}
	sort(stu, stu + n, cmp);
	cout << stu[0].name << ' ' << stu[0].chinese << ' ' << stu[0].math << ' ' << stu[0].english;
}

在这里插入图片描述

#include<iostream>
#include<algorithm>
#include<iomanip>
using namespace std;
struct stu
{
	string name;//string 比较的时候自动用字典序作比较
	int c;
	int m;
	int e;
}a[1000];
int main()
{
	int n, i, j, k;
	cin >> n;
	for (i = 0; i < n; i++)
	{
		cin >> a[i].name >> a[i].c >> a[i].m >> a[i].e;
	}
	for (int i = 0; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			int ans = abs(a[i].c - a[j].c);
			int ans1 = abs(a[i].m - a[j].m);
			int ans2 = abs(a[i].e - a[j].e);
			int ans3 = abs(a[i].e + a[i].c + a[i].m - a[j].c - a[j].m - a[j].e);
			if (ans <= 5 && ans1 <= 5 && ans2 <= 5 && ans3 <= 10) {
				if (a[i].name > a[j].name)
					cout << a[j].name << " " << a[i].name << "\n";
				else cout << a[i].name << " " << a[j].name << "\n";
			}
		}
	}
	return 0;
}

在这里插入图片描述

7
Bob 35
Amy 28
James 98
Alice 11
Jack 45
Smith 33
Chris 62
#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
struct stu
{
	char name[7];
	int num;
	int a;
	int sequence;
}s[10000];//数组一定要开到符合要求
bool cmp(stu c, stu b)
{
	if (c.a != b.a)return c.a < b.a;
	else if (c.a == b.a)return c.sequence < b.sequence;//当猜的数字一样的时候,else if这一段必须要加上,原因不明
}
int main()
{
	int n, i, j, k;double sum = 0, ave;
	cin >> n;
	for (i = 0; i < n; i++)
	{
		cin >> s[i].name >> s[i].num;
		sum += s[i].num;
		s[i].sequence = i;
	}
	ave = sum / n;
	ave = ave / 2.0;//变成小数也必须
	for (i = 0; i < n; i++)
	{
		s[i].a = abs(ave - s[i].num);
	}
	sort(s, s + n, cmp);
	cout <<int(ave) << ' ' << s[0].name;
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

每天都要学算法(努力版)

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

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

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

打赏作者

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

抵扣说明:

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

余额充值