洛谷:P5728 【深基5.例5】旗鼓相当的对手

# 【深基5.例5】旗鼓相当的对手

## 题目描述

现有 $N$ 名同学参加了期末考试,并且获得了每名同学的信息:语文、数学、英语成绩(均为不超过 $150$ 的自然数)。如果某对学生 $\lang i,j\rang$ 的每一科成绩的分差都不大于 $5$,且总分分差不大于 $10$,那么这对学生就是“旗鼓相当的对手”。现在想知道这些同学中,有几对“旗鼓相当的对手”?同样一个人可能会和其他好几名同学结对。

## 输入格式

第一行一个正整数 $N$。

接下来 $N$ 行,每行三个整数,其中第 $i$ 行表示第 $i$ 名同学的语文、数学、英语成绩。最先读入的同学编号为 $1$。

## 输出格式

输出一个整数,表示“旗鼓相当的对手”的对数。

## 样例 #1

### 样例输入 #1


3
90 90 90
85 95 90
80 100 91
 

### 样例输出 #1


2
 

答案:

纯享版(注释为打印):

//@author:SHARK_ddd
#include<iostream>
#include<vector>
using namespace std;

class Student
{
public:
    Student(int ch, int math, int eng) :m_ch(ch), m_math(math), m_eng(eng), m_sum(ch + math + eng) {}


    int m_ch;
    int m_math;
    int m_eng;
    int m_sum;
};

int main()
{
    vector<Student>stu;
    int n, m;
    int a, b, c;
    int i, j;
    int time = 0;
    cin >> n;
    m = n;
    while (n--)
    {
        cin >> a >> b >> c;
        stu.push_back(Student(a, b, c));
    }
    //for (vector<Student>::iterator it = stu.begin(); it != stu.end(); it++)
    //{
    //        cout << (*it).m_ch << "数学:" << (*it).m_math << "英语:" << (*it).m_eng << "总分:" << (*it).m_sum << endl;
    //}
    for (i = 0; i < m; i++)
    {
        for (j = i + 1; j < m; j++)
        {
            /* cout << abs(stu.at(i).m_ch - stu.at(j).m_ch);
            cout << abs(stu.at(i).m_math - stu.at(j).m_math);
            cout << abs(stu.at(i).m_eng - stu.at(j).m_eng);
            cout << abs(stu.at(i).m_sum - stu.at(j).m_sum); */

                if (abs(stu.at(i).m_ch - stu.at(j).m_ch) <= 5
                    && abs(stu.at(i).m_math - stu.at(j).m_math) <= 5
                    && abs(stu.at(i).m_eng - stu.at(j).m_eng) <= 5
                    && abs(stu.at(i).m_sum - stu.at(j).m_sum) <= 10)
                {
                    time++;
                }
        }
    }
    cout << time;
}

分析:
       

1. 创建一个student类来存放每个学生的三门成绩

        使用初始化列表来接收三门学科的成绩,并用sum记录每个学生的成绩总分。

class Student
{
public:
    Student(int ch, int math, int eng) :m_ch(ch), m_math(math), m_eng(eng), m_sum(ch + math + eng) {}


    int m_ch;
    int m_math;
    int m_eng;
    int m_sum;
};

2.  选择使用vector数组来存放各个数据

   用time来记录旗鼓相当的对手数(记录答案)

    vector<Student>stu;
    int time = 0;

3. 输入部分使用循环来实现三行三列数据的输入

        .push_back()是vector容器的赋值函数

        参数为Student这个成员函数

    while (n--)
    {
        cin >> a >> b >> c;
        stu.push_back(Student(a, b, c));
    }

4.输入之后可以打印测试一下是否正确

        使用迭代器来访问vector数组,并打印

    //for (vector<Student>::iterator it = stu.begin(); it != stu.end(); it++)
    //{
    //        cout << (*it).m_ch << "数学:" << (*it).m_math << "英语:" << (*it).m_eng << "总分:" << (*it).m_sum << endl;
    //}

5.数组内元素的遍历(使用两层for循环来实现)

注释部分仍然是通过打印来测试代码的正确性

    for (i = 0; i < m; i++)
    {
        for (j = i + 1; j < m; j++)
        {
            /* cout << abs(stu.at(i).m_ch - stu.at(j).m_ch);
            cout << abs(stu.at(i).m_math - stu.at(j).m_math);
            cout << abs(stu.at(i).m_eng - stu.at(j).m_eng);
            cout << abs(stu.at(i).m_sum - stu.at(j).m_sum); */

                if (abs(stu.at(i).m_ch - stu.at(j).m_ch) <= 5
                    && abs(stu.at(i).m_math - stu.at(j).m_math) <= 5
                    && abs(stu.at(i).m_eng - stu.at(j).m_eng) <= 5
                    && abs(stu.at(i).m_sum - stu.at(j).m_sum) <= 10)
                {
                    time++;
                }
        }
    }

6.条件的判断

                if (abs(stu.at(i).m_ch - stu.at(j).m_ch) <= 5
                    && abs(stu.at(i).m_math - stu.at(j).m_math) <= 5
                    && abs(stu.at(i).m_eng - stu.at(j).m_eng) <= 5
                    && abs(stu.at(i).m_sum - stu.at(j).m_sum) <= 10)
                {
                    time++;
                }

        abs为math头文件内的一个取整数绝对值的函数,以此来判断分值差值小于等于5

        stu为前面创建的vector类,

        .at()是数据按位访问的函数,

        .m_math这些是Student类中的成员变量。

7.输出

        当满足条件time++;

        最后打印time,输出答案

======结=================================束======

未完待续.................

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值