P1093 Scholarship (Logu)

Original title portal

Insert picture description here
Insert picture description here
Idea: First define five arrays of student number, Chinese, Mathematics, English, and then assign values ​​to the arrays, then use bubble sorting and if conditions to swap in three situations, and finally print out the top five student numbers and total scores. Note: The student ID starts from 1 and not 0!

Code reference

#include<iostream>
using namespace std;
int main(){
    
    
    int n,id[300],chinese[300],math[300],english[300],total[300];
    cin>>n;
    for(int i = 1;i <= n;i++){
    
    
        cin>>chinese[i]>>math[i]>>english[i];
        total[i] = chinese[i]+math[i]+english[i];
        id[i] = i;
    }
    for(int i = 0;i < n-1;i++){
    
    
        for(int j = 1;j <= n-i;j++){
    
    
            if(total[j] < total[j+1]){
    
    
                swap(total[j],total[j+1]);
                swap(chinese[j],chinese[j+1]);
                swap(id[j],id[j+1]);
            }
            if(total[j] == total[j+1])
                if(chinese[j] < chinese[j+1]){
    
    
                    swap(id[j],id[j+1]);
                    swap(chinese[j],chinese[j+1]);
                    swap(total[j],total[j+1]);
                }
            if((total[j] == total[j+1]) && (chinese[j] == chinese[j+1]))
                if(id[j] > id[j+1]){
    
    
                    swap(id[j],id[j+1]);
                    swap(chinese[j],chinese[j+1]);
                    swap(total[j],total[j+1]);
                }
        }
    }
    //打印出前五名的学号和总分
    for(int k = 1;k <= 5;k++)
        cout<<id[k]<<" "<<total[k]<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/Bertil/article/details/106817083