.现抽取我校附属中学六年级20名学生的期末总成绩,输出后,求高于平均值的学生成绩.(数据类型按照实际情况定义)
输入20个成绩,再求高于平均值的学生成绩
如有帮助请在我的回答上点个【采纳】
/* Note:Your choice is C IDE */
#include "stdio.h"
void main()
{
int a[20],i;
float avg=0,sum=0;
for(i=0;i<20;i++){
printf("请输入%d号学生成绩:",i+1);
scanf("%d",&a[i]);
sum+=a[i];
}
avg=sum/20.0;
printf("平均成绩为%.2f\n",avg);
printf("高于平均成绩的分数有:\n");
for(i=0;i<20;i++){
if(a[i]>avg){
printf("%3d",a[i]);
}
}
}
#include <stdio.h>
void main()
{
int i, arr[20];
int sum = 0;
int count = 0;
double avg = 0;
for (i = 0; i <20;++i)
{
scanf("%d", &arr[i]);
sum += arr[i];
}
avg = sum / 20.0;
for (i = 0; i <20;++i)
{
if (arr[i] > avg)
{
count++;
printf("高于平均分成绩:%d\n", arr[i]);
}
}
printf("平均分:%lf\n", avg);
printf("人数:%d\n", count);
}
我所理解题目的意思,写出的代码是这样的:
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef struct student {
string name; // 姓名
float chinese; // 语文成绩
float math; // 数学成绩
float english; // 英语成绩
float total; // 总成绩
};
int main(void) {
// 定义学生容器
vector<student> studentInfo;
// 定义数据
struct student stu1;
stu1.name = "小明";
stu1.chinese = 88.8;
stu1.math = 77.7;
stu1.english = 66.6;
stu1.total = (stu1.chinese + stu1.math + stu1.english) / 3;
struct student stu2;
stu2.name = "小红";
stu2.chinese = 82.8;
stu2.math = 74.7;
stu2.english = 68.6;
stu2.total = (stu2.chinese + stu2.math + stu2.english) / 3;
struct student stu3;
stu3.name = "小蓝";
stu3.chinese = 68.8;
stu3.math = 87.7;
stu3.english = 96.6;
stu3.total = (stu3.chinese + stu3.math + stu3.english) / 3;
struct student stu4;
stu4.name = "小紫";
stu4.chinese = 67.8;
stu4.math = 89.7;
stu4.english = 98.6;
stu4.total = (stu4.chinese + stu4.math + stu4.english) / 3;
struct student stu5;
stu5.name = "小黄";
stu5.chinese = 88.5;
stu5.math = 87.5;
stu5.english = 90.0;
stu5.total = (stu5.chinese + stu5.math + stu5.english) / 3;
// 结构体数据存储到容器中
studentInfo.push_back(stu1);
studentInfo.push_back(stu2);
studentInfo.push_back(stu3);
studentInfo.push_back(stu4);
studentInfo.push_back(stu5);
// 计算总平均分
double avg = 0.0;
for (int i = 0; i < studentInfo.size(); i++) {
avg += studentInfo.at(i).total;
cout << "name:" << studentInfo.at(i).name << "\t总成绩:" << studentInfo.at(i).total << endl;
}
// 计算他们平均分的平均分
avg /= studentInfo.size();
cout << "平均总分:" << avg << endl;
// 输出高于总平均分的学生成绩
cout << endl << "平均分高于总成绩的学生:" << endl;
for (int i = 0; i < studentInfo.size(); i++) {
if (studentInfo.at(i).total > avg) {
cout << "name:" << studentInfo.at(i).name << "\t总成绩:" << studentInfo.at(i).total << endl;
}
}
return 0;
}
不知道符不符合你的需求,如果符合,请点个采纳!