公子


私信TA

用户名:oyss

访问量:393

签 名:

等  级
排  名 18957
经  验 686
参赛次数 0
文章发表 1
年  龄 0
在职情况 学生
学  校 江西师范大学
专  业

  自我简介:

TA的其他文章

奖学金解题
浏览:294

解题思路:

题目讲述的非常清楚,输入几个人和他们的成绩与学号,在按照他们的总分,语文成绩或者学号进行排名,所以应运用结构体,在编写几个子函数就可简便的做出题解,以下见代码参考

注意事项:
算总分的子函数要用一级指针。

total预先设为0否则后面运算的结果可能出错。
参考代码:

#include <iostream>

 int n;

typedef struct student{

int score[3];

int total=0;

int m;

}student;


void calTotal(student* ps){

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

ps->total += ps->score[i];

}

void exchange(student* x, student* y){

student temp = *x;

*x = *y;

*y = temp; 

}

void sort(student s[]){

for(int j=n-1;j>0;j--){

    for(int i=0;i<j;i++){

        if(s[i].total<s[i+1].total)

            exchange(&s[i], &s[i+1]);

        if(s[i].total==s[i+1].total){

            if(s[i].score[0]<s[i+1].score[0])

                exchange(&s[i], &s[i+1]);

            if(s[i].score[0]==s[i+1].score[0])

                if(s[i].m>s[i+1].m)

                      exchange(&s[i], &s[i+1]);

}

}

}

}

int main(){

    scanf("%d",&n);

student s[n];

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

scanf("%d %d %d",&s[i].score[0],&s[i].score[1],&s[i].score[2]);

s[i].m=i+1;

}

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

calTotal(s+i);

sort(s);

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

printf("%d %d\n",s[i].m,s[i].total);

return 0;

}


 

0.0分

2 人评分

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

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区