天天看點

[PAT][Basic level] 1015 code review

Not AC Code:(one problem to be resolved)

KeyWrods: sort();bool cmp();#include “algorithm”;the diffirence between qsort() and sort();struct;typedef (關鍵字替換);

#include <cstdio>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <cmath>
#include <algorithm>
using namespace std;
typedef struct student{
  int number;
  int score_de;
  int score_cai;
  int classy = 0;
  int sum = 0;
}student;

void print (student * stu,int classy ,int n,int count_class)  ;
bool cmp( struct student a , struct student b);
int main()
{
    int n,l,h;
    int m=0;
    int count_class1 = 0,count_class2 = 0 ,count_class3 = 0,count_class4 = 0; 
    cin>>n>>l>>h;
    student * stu;
    stu = (student *)malloc(sizeof(student)*n);
    for( int  i = 0 ; i < n ; i ++){
        cin>>stu[i].number>>stu[i].score_de>>stu[i].score_cai;
        stu[i].sum = stu[i].score_de + stu[i].score_cai ;   //sum
        
        if(stu[i].score_de > l &&stu[i].score_cai > l || stu[i].score_de == l|| stu[i].score_cai == l){
            m ++;
            
            if(stu[i].score_de > h  ||stu[i].score_de == h ){
                if(stu[i].score_cai > h || stu[i].score_cai == h){
                    stu[i].classy = 1 ;
                    count_class1 ++;
                }
                else {
                    stu[i].classy = 2 ;
                    count_class2 ++;
                }
                    
            }
            else if(stu[i].score_de > stu[i].score_cai || stu[i].score_de == stu[i].score_cai){
                stu[i].classy = 3;
                count_class3  ++;
            }
            else {
                stu[i].classy = 4;
                count_class4 ++;
            }
            
        }
        
        
    }
    
    cout<<m<<endl;
    
    print(stu,1,n,count_class1);//classy == 1
    print(stu,2,n,count_class2);
    print(stu,3,n,count_class3);
    print(stu,4,n,count_class4);

    return 0;
}

void print (student * stu,int classy ,int n,int count_class)  {
    
    student * stu_output ;
    stu_output = (student *) malloc(sizeof(student)*count_class);
    int j = 0 ;
    for(int i = 0 ; i < n ; i ++){
        if(stu[i].classy == classy){
            stu_output[j++] = stu[i] ;  
        }
    }
    
    sort(stu_output,stu_output+count_class,cmp);
    for(int i = 0 ; i < count_class ; i ++){
        cout<<stu_output[i].number<<" "<<stu_output[i].score_de<<" "<<stu_output[i].score_cai<<endl;
    }
    //cout<<endl;
    return ;
}
bool cmp( struct student a , struct student b){
    if(a.sum < b.sum){
        return a.sum > b.sum ;
    }
    else if (a.sum == b.sum ){
        if(a.score_de < b.score_de){
            return a.score_de > b.score_de;
        }
        else if (a.score_de == b.score_de){
            if(a.number > b.number){
                return a.number < b.number;
            }
        }
    }
}