c语言,可以是第一个跟第二个一块的,也可以是第二个改自我的第一个的

img


这两个题是一个,第一个做出来了第二个不会

img


c语言,可以是第一个跟第二个一块的,也可以是第二个改自我的第一个的。

使用循环比较不同同学的成绩平均值,如果平均值不同再比较学号,然后交换两个比较的学生结构应该就可以了。

参考链接:


https://www.cnblogs.com/990924991101ywg/p/10771972.html

#include  <stdio.h>
#include <string.h>
struct stu{
    
    char no[20];  
    int chinese;
    int math;
    int english;
    float ave;
    
}; 

void sort(struct stu*p,int n){
    
    struct stu  temp;
    // 根据题目要求,在函数中计算其结构数组中每个学生的成绩 
    for(int i=0;i<n;i++){
        (p+i)->ave=((p+i)->chinese+(p+i)->math+(p+i)->english)/3.0;
    
    }
    
    
    
    //  https://zhuanlan.zhihu.com/p/29889599
    // 采用选择排序来排序结构
    for(int i=0;i<n-1;i++){
    
        for(int j=i+1;j<n;j++){
            
            // 按成绩从大到小排序结构 
    // 如果前面i位置的结构中的平均成绩 小于  后面j位置的结构中的平均成绩
    // 则交换两个结构 
            if((p+i)->ave < (p+j)->ave){ 
                
                temp=*(p+i);
                *(p+i)=*(p+j);
                *(p+j)=temp;
        
        
            }
        
            // 如果成绩相同,则按学号从小到大排序结构 
            // 如果前面i位置的结构中的平均成绩 等于  后面j位置的结构中的平均成绩
            //  并 前面i位置的结构中的学号 大于  后面j位置的结构中的学号 
            //  则交换前后两个结构 
            // https://www.cnblogs.com/990924991101ywg/p/10771972.html 
            if(  (p+i)->ave == (p+j)->ave  ){
                
                
                if( strcmp((p+i)->no , (p+j)->no)>0){
                    temp=*(p+i);
                    *(p+i)=*(p+j);
                    *(p+j)=temp;
                
                }
                
                
            }
            
        }
        
                    
        }
        

    
    
}

int main(void){
    
    struct stu students[3];
    struct stu *pstu;
    
    printf("请输入3个学生的学号和成绩(语文、数学、英语):\n");
    for(int i=0;i<3;i++){
        pstu=&students[i];
        scanf("%s%d%d%d",pstu->no,&(pstu->chinese),&(pstu->math),&(pstu->english));
    //    pstu->ave=(pstu->chinese+pstu->math+pstu->english)/3.0;
    
    }

    sort(&students[0],3);
    
    printf("学号\t语文\t数学\t英语\t平均成绩\n");
    for(int i=0;i<3;i++){
        pstu=&students[i];
        printf("%s\t%d\t%d\t%d\t%.1f\n",pstu->no,pstu->chinese,pstu->math,pstu->english,pstu->ave);
    }
    return 0;
}

img