用c++编程 求出n个学生一门课程的最好成绩、最低成绩及高于平均成绩的人数?
1个回答
展开全部
#include <iostream>
using namespace std;
int n,i=0,j,temp,count=0,sum=0;
double avg;
void inputNunbers(){
cout<<"请输入学生人数:";
cin>>n;
if(n<=0){
cout<<"学生人数必须大于0!\n"<<endl;
inputNunbers();
}
}
int *scores=new int[n];
void inputScore(){
for(;i<n;){
if(i==n){
break;
}else{
cout<<"请输入第"<<(i+1)<<"个学生成绩:";
cin>>scores[i];
if(scores[i]<0||scores[i]>100){
cout<<"成绩必须大于等于0,小于等于100!\n"<<endl;
if(i==n){
break;
}
inputScore();
}else{
sum+=scores[i];
i++;
}
}
}
}
void main(){
inputNunbers();
inputScore();
for(i=0;i<n-1;i++){
for(j=0;j<n-i-1;j++){
if(scores[j]>scores[j+1]){
temp=scores[j];
scores[j]=scores[j+1];
scores[j+1]=temp;
}
}
}
avg = sum*1.0/n;
for(i=0;i<n;i++){
if(scores[i]>avg){
count++;
}
}
cout<<"最好成绩是:"<<scores[n-1]<<",最差成绩是:"<<scores[0]<<",高于平均成绩的有:"<<count<<"人!"<<endl;
}
using namespace std;
int n,i=0,j,temp,count=0,sum=0;
double avg;
void inputNunbers(){
cout<<"请输入学生人数:";
cin>>n;
if(n<=0){
cout<<"学生人数必须大于0!\n"<<endl;
inputNunbers();
}
}
int *scores=new int[n];
void inputScore(){
for(;i<n;){
if(i==n){
break;
}else{
cout<<"请输入第"<<(i+1)<<"个学生成绩:";
cin>>scores[i];
if(scores[i]<0||scores[i]>100){
cout<<"成绩必须大于等于0,小于等于100!\n"<<endl;
if(i==n){
break;
}
inputScore();
}else{
sum+=scores[i];
i++;
}
}
}
}
void main(){
inputNunbers();
inputScore();
for(i=0;i<n-1;i++){
for(j=0;j<n-i-1;j++){
if(scores[j]>scores[j+1]){
temp=scores[j];
scores[j]=scores[j+1];
scores[j+1]=temp;
}
}
}
avg = sum*1.0/n;
for(i=0;i<n;i++){
if(scores[i]>avg){
count++;
}
}
cout<<"最好成绩是:"<<scores[n-1]<<",最差成绩是:"<<scores[0]<<",高于平均成绩的有:"<<count<<"人!"<<endl;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询