Chapter 2 Data Sorting of Xin Olympiad Algorithm ([Example 2.2] Car Reorganization, Who Passed the Kth Place in the Examination, Odd Single Increasing Sequence, Score Sorting, Scholarships, Score Line Demarcation, Integer Odd and Even Sorting)

1310: [Example 2.2] Carriage reorganization

Insert image description here
This question is simple. The question talks about adjacent value exchange, and it is a sorting algorithm. So as soon as we infer it, we know that it is bubble sort.
Then just exchange the values, let us add 1 to the count, and finish the output after sorting!

//1310:【例2.2】车厢重组
#include<bits/stdc++.h>
using namespace std;
int a[10001];
int main(){
    
    
	int n,cnt=0;
	cin>>n;
	for(int i=0;i<n;i++){
    
    
		cin>>a[i];
	}
	for(int i=n-1;i>=0;i--){
    
    
		bool flag=false;
		for(int j=0;j<i;j++){
    
    
			if(a[j]>a[j+1]){
    
    
				swap(a[j],a[j+1]);
				flag=true;
				cnt++;
			}
		}
		if(flag==false){
    
    
			break;
		}
	}
	cout<<cnt;
	return 0;
} 

1176: Who passed the kth exam?

Insert image description here
It can be seen from the question that a student has two attributes, student number and grade; we can use a structure to save the result.
After the definition and input are completed, we need to rank the scores from high to low.
Sort sorting and custom comparison function cmp are required.
Finally, output the result of Kth place.

#include<bits/stdc++.h>
using namespace std;
struct Student{
    
    
    int name;  // 学生名字 
    float score; // 存三个成绩 语数英 
}; 
bool cmp(Student x,Student y){
    
    
	return x.score>y.score;
}
int main(){
    
    
	int n,k;
	cin>>n>>k;
	Student stu[n+1];
	for(int i=0;i<n;i++){
    
    
		cin>>stu[i].name>>stu[i].score;
	}
	sort(stu,stu+n,cmp);
	cout<<stu[k-1].name<<" "<<stu[k-1].score;
	return 0;
}

1177: Odd single increasing sequence

Insert image description here
Insert image description here
Input values ​​into the array, then sort, use sort to sort, cmp custom comparison: odd numbers first, even numbers last. The output requires commas. We can output a value with a comma. Note that the loop output only reaches the second to last number.

//1177:奇数单增序列
#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
	int n,a[1000],j=0;
	cin>>n;
	for(int i=0;i<n;i++){
    
    
		int k;
		cin>>k;
		if(k%2) a[j++]=k;
	}
	sort(a,a+j);
	for(int i=0;i<j-1;i++){
    
    
		cout<<a[i]<<",";
	}
	cout<<a[j-1];
	return 0;
} 

1178: Score sorting

Insert image description here
Insert image description here
As can be seen from the title, a student has two attributes: name and grade, and we use a structure to store them.

  1. Define the structure, then declare the structure array and enter the values.
  2. Sort the students' scores, and customize the comparison function to order the scores from large to small. If the scores are equal, the names will be arranged alphabetically from small to large.
  3. Finally output the result.
//1178:成绩排序
#include<bits/stdc++.h>
using namespace std;
struct Student{
    
    
    string name;  // 学生名字 
    int score; 
}; 
bool cmp(Student x,Student y){
    
    
	if(x.score!=y.score) return x.score>y.score;
	else return x.name<y.name;
}
int main(){
    
    
	int n;
	cin>>n;
	Student stu[n+1];
	for(int i=0;i<n;i++){
    
    
		cin>>stu[i].name>>stu[i].score;
	}
	sort(stu,stu+n,cmp);
	for(int i=0;i<n;i++){
    
    
		cout<<stu[i].name<<" "<<stu[i].score<<endl;
	}
	return 0;
}

1179: Scholarship

Insert image description here
As can be seen from the title, one of our students has 5 attributes including Chinese, Mathematics and English scores, student number, and total score. Then we define the structure to complete.

  1. Define the structure, then declare the structure array and enter the value, and assign the student number.
  2. Sort the total score, Chinese score, and student number in sequence. The custom comparison function makes the total score go from high to low. If the total scores are equal, the Chinese score goes from high to low. Otherwise, the student number is sorted from small to high.
  3. Finally, the top 5 student numbers and total scores are output.
//1179:奖学金
#include<bits/stdc++.h>
using namespace std;
struct Student {
    
    
    int score[3],total,id;
};
bool cmp(Student x,Student y){
    
    
	if(x.total!=y.total) 
		return x.total>y.total;
	if(x.total==y.total && x.score[0]!=y.score[0]) 
		return x.score[0]>y.score[0];
	if(x.total==y.total && x.score[0]==y.score[0])
		return x.id < y.id;
}
int main() {
    
    
	int n;
	cin>>n;
    Student stu[n+1];
    for (int i=0; i<n; i++){
    
    
    	stu[i].id = i+1;
    	stu[i].total=0; 
    	for(int j=0;j<3;j++){
    
    
    		cin>>stu[i].score[j];
    		stu[i].total += stu[i].score[j];	
		}
    }
    sort(stu,stu+n,cmp);
    for(int i=0; i<5; i++){
    
     
        cout<<stu[i].id<<" "<<stu[i].total<<endl;
    }
    
    return 0;
}

1180: Demarcation of score lines

Insert image description hereInsert image description hereInsert image description here
As can be seen from the question, the interview score cutoff is the Mth place x 150% of the score cutoff. We know that a student has two attributes, student number and grade, which are made using a structure.

  1. Define the structure, then declare the structure array and enter the values.
struct Student{
    
    
    int id;   
    int score; 
}; 
int main(){
    
    
	int n,m;
	cin>>n>>m;
	Student stu[n+1];
	for(int i=0;i<n;i++){
    
    
		cin>>stu[i].id>>stu[i].score;
	}
  1. After the interview, the candidates with the best scores must be admitted first, which means we must sort the scores in advance. Results are consistently sorted by student number from smallest to largest.
bool cmp(Student x,Student y){
    
     //主函数外
	if(x.score!=y.score) return x.score>y.score;
	return x.id<y.id;
}
//主函数内
sort(stu,stu+n,cmp);
  1. After sorting, find the score of mth x 150% and store it in the p variable
int cnt=0, p = stu[int(m*1.5)-1].score; //分数向下取整
  1. Then find the number of people cnt larger than the p-th score line. Compare it in a loop. If it is greater than or equal to p, the number will be +1.
for(int i=0;i<n;i++){
    
    
		if(stu[i].score>=p){
    
    
			cnt++;
		}
	}
  1. Finally, the student number and grades of the cnt name are output in sequence.
cout<<stu[cnt-1].score<<" "<<cnt<<endl;
	for(int i=0;i<cnt;i++){
    
    	
		cout<<stu[i].id<<" "<<stu[i].score<<endl;
	}

The total code is as follows:

//1180:分数线划定
#include<bits/stdc++.h>
using namespace std;
struct Student{
    
    
    int id;   
    int score; 
}; 
bool cmp(Student x,Student y){
    
    
	if(x.score!=y.score) return x.score>y.score;
	return x.id<y.id;
}
int main(){
    
    
	int n,m;
	cin>>n>>m;
	Student stu[n+1];
	for(int i=0;i<n;i++){
    
    
		cin>>stu[i].id>>stu[i].score;
	}
	sort(stu,stu+n,cmp);
	
	int cnt=0, p = stu[int(m*1.5)-1].score;
	for(int i=0;i<n;i++){
    
    
		if(stu[i].score>=p){
    
    
			cnt++;
		}
	}
	cout<<stu[cnt-1].score<<" "<<cnt<<endl;
	for(int i=0;i<cnt;i++){
    
    	
		cout<<stu[i].id<<" "<<stu[i].score<<endl;
	}
	return 0;
}

1181: Integer odd-even sorting

Insert image description here
As you can see from the question, define a one-dimensional array and enter 10 numbers. Then sort and customize the comparison process.

//1181:整数奇偶排序
#include <iostream>
#include <algorithm>
using namespace std;
bool mycmp (int a,int b){
    
    
    if(a % 2 != b % 2){
    
    
        return a % 2 > b % 2;
    }
    if(a % 2 == b % 2){
    
    
    	if(a%2!=0) return a>b;
		else  return a<b;
	}
}
int main() {
    
    
    int arr[11],i=0;
//    while(cin>>arr[i++]){
    
    
//    	;
//	}
	for(int i=0;i<10;i++){
    
    
		cin>>arr[i];
	}
    sort(arr,arr + 10,mycmp);
    for (int i = 0; i < 10; i++) {
    
    
        cout << arr[i] <<" ";
    }
    cout << endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44775255/article/details/134017782