Note: PAT-B1032 - which is the best excavator technology

Posted by pbarney on Sun, 23 Jan 2022 17:07:10 +0100

PAT-B1032 - which excavator technology is better

Title (20 points)

In order to prove which excavator technology is better, PAT organized an excavator skills competition. Now please count the school with the strongest technology according to the competition results.

input

Enter a positive integer N that does not exceed 10 * * 5 on line 1, that is, the number of participants. Then N lines, each line gives the information and results of one contestant, including the number of the school it represents (consecutive numbers from 1) and its competition results (percentile system), separated by spaces.

6
3 65
2 80
1 100
2 70
3 40
3 0
output

In one line, give the number of the school with the highest total score and its total score, separated by spaces. The questions ensure that the answers are unique without juxtaposition.

2 150
My approach
  1. Define a structure, and take the number and grade as the data of a structure;
  2. Use scanf to input each data (i.e. student's school number and grade) into the structure array;
  3. Traverse the array, add the results with the same number, only retain the number data encountered for the first time, and set the remaining numbers to zero. After the end, you will get the same effect as de duplication;
  4. Traverse the array once, select the one whose number is not 0 and whose total score is the highest, and then output it.
#include<stdio.h>

struct stuInfo{                     //Define student achievement structure
    int index;                      //number
    int mark;                       //achievement
    stuInfo(){}                     //Nonparametric construction method
    stuInfo(int _index,int _mark){  //Custom construction method
        index=_index;
        mark=_mark;
    }
};

int main(){
    /*Input data*/
    int t;              //Number of cycles, i.e. number of students
    scanf("%d",&t);     
    int stuLen=t;       //Save a copy for later use
    stuInfo stu[t];     //Initialize an array stu that holds the student structure   
    while(t--){		    //Enter data one by one 
        int a,b;
        scanf("%d %d",&a,&b);
        stu[t]=stuInfo(a,b);
    }
    
    /*Consolidate and calculate the total score*/
    for(int i=0;i<stuLen;i++){      
    	if(stu[i].index==0){                //If the number is 0, the cycle will be ended directly
	    	continue;
	    } 
        for(int j=i+1;j<stuLen;j++){
			if(stu[i].index==stu[j].index){
  				stu[i].mark+=stu[j].mark;       //If the numbers are the same, add the score to the previous array element
     			stu[j].index=0;	                //Set the merged number to 0
       		} 
        }  
    }
    
    /*Traversal comparison size*/
    int max=0,num=0;
    for(int k=0;k<stuLen;k++){
    	if(stu[k].index==0){    //If the number is 0, this cycle will be skipped
	    	continue;
	    } 
        if(stu[k].mark>max){	
            max=stu[k].mark;
            num=stu[k].index;
        }
    }
    
    /*output data*/
    printf("%d %d",num,max);
    return 0;
}
Score results

20 points, 15 points, one wrong answer and one timeout. Although I think there is room for optimization, I don't know where to optimize.

Look at the answer. AC is successful after modification
#include<stdio.h>

struct stuInfo{                     //Define student achievement structure
    int index;                      //number
    int mark;                       //achievement
    stuInfo(){}                     //Nonparametric construction method
    stuInfo(int _index,int _mark){  //Custom construction method
        index=_index;
        mark=_mark;
    }
};

int main(){
    /*Input data*/
    int t;              //Number of cycles, i.e. number of students
    scanf("%d",&t);     
    stuInfo stu[t];     //Initialize an array stu that holds the student structure
    for(int i=0;i<t;i++){
        int a,b;
        scanf("%d %d",&a,&b);
        stu[i]=stuInfo(a,b);
    }
    
    /*Array summation*/
    int amout[100000]={0};  //Initialize an array to store scores
    for(int i=0;i<t;i++)
        amout[stu[i].index]+=stu[i].mark;
    	//The number corresponds to the subscript. This can be done because the school number is continuous from 1
    
    /*Traversal comparison size*/   
    int max=-1,num=1;           //The school number is at least 1, so the initial value can only be 1
    for(int i=1;i<=t;i++){      //The school number starts from 1, so the initial value of i is 1
        if(max<amout[i]){
            max=amout[i];
            num=i;
        }
    }

    /*output*/
    printf("%d %d\n",num, max);
    return 0;
}

summary
  1. The answer to this question is to use arrays directly. Both the amount of code and complexity are much simpler than mine.
  2. However, I think my initial idea of solving the problem is also correct. In line 39 of the initial code, after modifying the initial value of num to 1, I got 17 points, and 3 points were deducted because of running timeout.
  3. After referring to the solution of the answer, the reason for the error is that it does not pay attention to the condition that the school number increases continuously from 1.

Two phase comparison

  1. My method is a little clumsy. The nesting of the for loop also raises the time complexity to a higher level, but the advantage is that it can save about 2 / 3 of the space and has better universality. It can accept the discontinuous school number, and the use of structure also makes the program more readable, and the disadvantage is easy to timeout.
  2. The answer method only uses arrays. It has advantages in time and concise code, but the utilization rate of space is not high. I don't like it.

Topics: C Algorithm data structure