1180:成绩统计(结构体)

总时间限制: 1000ms 内存限制: 128000kB

描述

某班级有n人(n<80),期末考试的六门学科分别是语文、数学、英语、物理、化学、生物。考试成绩出来了,现要求每人的成绩总分和各学科的平均分。输入班级人数,每人的座号和各学科成绩,输出每人的座号、成绩和各学科平均分(四舍五入保留1位小数)

输入

第一行一个整数n,表示n个人
以下n行,每行7个整数,分别表示座号和语文、数学、英语、物理、化学、生物的成绩。

输出

输出n行整数,每行8个整数,分别表示座号和语文、数学、英语、物理、化学、生物、总分的成绩。
最后一行是语文、数学、英语、物理、化学、生物的平均分(保留1位小数)。

样例输入

3
1 67 89 93 82 87 90
2 80 98 87 82 89 93
3 78 86 92 90 67 85

样例输出

1 67 89 93 82 87 90 508
2 80 98 87 82 89 93 529
3 78 86 92 90 67 85 498
75.0 91.0 90.7 84.7 81.0 89.3

#include <iostream>
#include <cstdio>
#include <iomanip>

using namespace std;

struct garde
{
  double id;
  double yuwen;
  double shuxue;
  double yingyu;
  double wuli;
  double huaxue;
  double shengwu;
  double sum;

};
int main()
{
    int n,i,j;
    double ave[6]={0,0,0,0,0,0};
    garde a[80];
    cin>>n;
        for(i=0;i<n;i++)
        {
            cin>>a[i].id>>a[i].yuwen>>a[i].shuxue>>a[i].yingyu
            >>a[i].wuli>>a[i].huaxue>>a[i].shengwu;//输入
            
        a[i].sum=a[i].yuwen+a[i].shuxue+a[i].yingyu
            +a[i].wuli+a[i].huaxue+a[i].shengwu;//总分
            
           cout<<a[i].id<<" "<<a[i].yuwen<<" "<<a[i].shuxue<<" "<<a[i].yingyu<<" "
            <<a[i].wuli<<" "<<a[i].huaxue<<" "<<a[i].shengwu<<" "<<a[i].sum<<endl;//输出

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

        {ave[0]+=a[i].yuwen;//各科总分
        ave[1]+=a[i].shuxue;
        ave[2]+=a[i].yingyu;
        ave[3]+=a[i].wuli;
        ave[4]+=a[i].huaxue;
        ave[5]+=a[i].shengwu;}
    for(j=0;j<6;j++)
    cout<<fixed<<setprecision(1)<<ave[j]/n<<" ";
}

——————————
总感觉还是有更好的方法,比如在结构体中顶一个数组。
struct student
{
int id;
double point[7]
};

——————————————————————-
刚才尝试了一下,可以
代码给大家贴出来



#include <iostream>
#include <cstdio>
#include <iomanip>

using namespace std;

struct student
{
  double id;
  double point[7];//各科成绩

};
int main()
{
    int n,i,j;
    double ave[6]={0,0,0,0,0,0};//各科平均
    student a[80];
    cin>>n;
        for(i=0;i<n;i++)
        {a[6].point[6]=0;
        cin>>a[i].id;
            for(j=0;j<6;j++)
        {
            cin>>a[i].point[j];
        a[i].point[6]+=a[i].point[j];

        }
        }
        for(i=0;i<n;i++)
        {
            cout<<a[i].id<<" ";//输出
            for(j=0;j<7;j++)
        {
            cout<<a[i].point[j]<<" ";
        }
        cout<<endl;
        }

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

        {ave[0]+=a[i].point[0];
        ave[1]+=a[i].point[1];
        ave[2]+=a[i].point[2];
        ave[3]+=a[i].point[3];
        ave[4]+=a[i].point[4];
        ave[5]+=a[i].point[5];}

 for(j=0;j<6;j++)
    cout<<fixed<<setprecision(1)<<ave[j]/n<<" ";


}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C++语言中使用结构体进行军事体能成绩统计的示例代码: ```cpp #include <iostream> using namespace std; struct Student { string name; int age; float height; float weight; float score; }; int main() { const int N = 5; // 学生人数 Student students[N]; // 输入学生信息 for (int i = 0; i < N; i++) { cout << "请输入第" << i+1 << "个学生的信息:" << endl; cout << "姓名:"; cin >> students[i].name; cout << "年龄:"; cin >> students[i].age; cout << "身高(米):"; cin >> students[i].height; cout << "体重(千克):"; cin >> students[i].weight; cout << "体能成绩:"; cin >> students[i].score; } // 计算平均值 float avg_height = 0, avg_weight = 0, avg_score = 0; for (int i = 0; i < N; i++) { avg_height += students[i].height; avg_weight += students[i].weight; avg_score += students[i].score; } avg_height /= N; avg_weight /= N; avg_score /= N; // 输出结果 cout << "平均身高:" << avg_height << "米" << endl; cout << "平均体重:" << avg_weight << "千克" << endl; cout << "平均体能成绩:" << avg_score << endl; return 0; } ``` 该程序定义了一个名为`Student`的结构体,包含了学生的姓名、年龄、身高、体重和体能成绩等信息。程序首先定义了一个长度为5的`students`数组,用于存储5个学生的信息。然后通过循环输入每个学生的信息。最后,程序计算出所有学生的平均身高、平均体重和平均体能成绩,并输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值