私信TA

用户名:aj1433223

访问量:961

签 名:

等  级
排  名 3754
经  验 1772
参赛次数 1
文章发表 7
年  龄 20
在职情况 学生
学  校 上饶职业技术学院
专  业 计算机应用技术

  自我简介:

C 艹

解题思路:  

定义学生类(然后初始化N个<100),

每个类有属性 :学号,姓名、三科成绩 :string num,name、 vector容器



注意事项:   

参考代码:

#include <iostream>

#include <vector>

using namespace std;

class student

{

public:

    string num, name;

    vector<int>v;

    student() { };

    ~student() { };


//1、 输入 学生数据

    void input()  

    {   

        int n = 0;

        cin >> this->num >> this->name;

        for (int i = 0; i < 3; i++)

        {

            cin >> n;

            this->v.push_back(n);

        }

    }

//2、 输出 学生数据 

    void output()

    {

        cout << this->num << ' ' << this->name;

        for(vector<int>:: iterator it=this->v.begin(); it!=this->v.end(); it++)

        cout << ' ' << *it;

    }

//3、 返回该学生,成绩总和

    int sum()

    {

        int sum2 = 0;

        for (vector<int>:: iterator it = this->v.begin();  it != this->v.end(); it++)

        sum2 += *it;


        return sum2;

    }

};

//4、 输出 每门课 平均分

void average(student *s, const int n)

{

   /* 指针偏移后,再次使用要复位,否则 内存溢出

    方法一 :记录偏移量 : 指针++,也变量n++,复位 :“ p -= n”

    方法二 :备份 指针 : 复位 :“p = tempS” */

      student* tempS = s;  

      int sum = 0;


     for (int i = 0; i < 3; i++) // 每门学科

    {

        for (int j = 0; j < n; j++) // 的所有学生

       {

            sum += s->v.at(i);

            s++;

       }

       s = tempS;   // 指针复位( 否则 内存溢出 )


       sum /= n;     // 每门课 的平均值

       cout << sum << ' ';

        sum = 0;

    }

     cout << endl;

 }


//5、 返回 总分最高分学生 下标

int max(student* s, const int n)

{

    int sum = 0, temp = 0, max = 0;

    for (int i = 0; i < n; i++) // 每个学生

    {

        if (s->sum() > temp)

        max = i; // 更新最高分 学生类 数组下标

        temp = s->sum();

        s++;

    }

    return max;

}


int main()

{

    int n;

    student s[100];  // 初始化 100个类 数组

    cin >> n;

    for (int i = 0; i < n; i++)

        s[i].input();                  // 输入 n个 学生数据


    average(s, n);                   // 输出 每门课 平均分


    int maxs = max(s, n);     // 输出 最高分学生 数据

    s[maxs].output();

    return 0;

}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区