西安电子大学计算机考研复试机试(2019)+ 算法笔记(入门二)+选择排序+插入排序+sort()的例子(PAT A1025)

选择排序+插入排序

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;


/*
选择排序 :每次找最小值往前放,前面构成一个有序序列 
*/ 
void select_sort(int a[],int n){
	for(int i=0;i<n;i++){
		int k = i;
		for(int j=i;j<n;j++){
			if(a[j]<a[k]){
				k = j; // 此处只需要找到最小值下标 ,不需要交换什么 
			}			
		}
		int temp = a[i];
		a[i] = a[k];
		a[k] = temp;
	}
} 
/*
插入排序:前部分序列是有序的,将后面的逐个往前面插 
*/
void insert_sort(int a[],int n){
	for(int i=1;i<n;i++){
		int temp = a[i],j = i;// 需要设置一个哨兵
		while(j>0&&temp<a[j-1]){// 注意这里的temp不能换成a[i],虽然i变量没有变,但是下面的语句 可能已经将a[j-1]赋值给了a[i]这个位置 	
			a[j] = a[j-1]; // 前面给后面腾地方 
			j--; // 找到适合插入的位置 
		}
		a[j] = temp;
	}
	
} 
int main(){
	int n ; 
	while(cin>>n){
		int a[n];
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
	//	select_sort(a,n);
		insert_sort(a,n); 
		for(int i=0;i<n;i++){
			cout<<a[i]<<" ";
		}
		cout<<endl;
	} 
	return 0;
} 

排序题以及sort函数的应用

对于某些题目中给出个体的许多信息。

  1. 比如学生的学号,姓名,成绩,排名等项,需要使用结构体定义这一学生变量。
  2. 对学生成绩进行排序时,可以直接调用 sort(a,a+n,cmp) 函数【头文件 :#include<algorithm>】,自定义cmp判断规则。
  3. 可以定义结构体数组存储多个学生,也可以用vector容器【相当于数组】,这个比较方便好用。

        vector<Student> v(n); // n表示元素个数,每一个元素是Student结构体类型。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;

struct Student{
	char name[20]; // 姓名 
//	char id[10];   // 考号 
	int score;     // 成绩 
	int rank;      // 排名 
}stu[10010];

// 使用sort函数排序,定义的cmp规范函数
// 成绩相同时,将姓名按照字典序排列 
bool cmp(Student a,Student b){
	if(a.score != b.score){
		return a.score > b.score;
	}else{
		return strcmp(a.name,b.name)<0;
	}
} 
/*
input:
3
Alice 1909080721 89 1
Tom 1909080732 89 1
Sam 1909080712 99 1
*/

int main(){
	int n ;
	cin>>n;
	vector<Student> t(n); //定义一个容量为n的容器 
	for(int i =0;i<n;i++){
		cin>>t[i].name>>t[i].score>>t[i].rank;
	} 
	sort(t.begin(),t.end(),cmp);
	for(int i=0;i<n;i++){
		cout<<" "<<t[i].name<<" "<<t[i].score<<" "<<t[i].rank<<endl; 
	}   
	// 成绩排好序之后,算排名 
	for(int i= 0;i<n;i++){
		t[i+1].rank = t[i].rank;
		if(t[i].score != t[i+1].score){
			t[i+1].rank=i+1;
		}
	}
	cout<<"rank:"<<endl;
	for(int i=0;i<n;i++){
		cout<<" "<<t[i].name<<" "<<t[i].score<<" "<<t[i].rank<<endl; 
	} 
	return 0;
} 

1025 PAT Ranking (25 分)

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank

The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

Sample Input:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

Sample Output:

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4

这道题目涉及到多个考场,需要有一个考场的内排名,还要有一个全校的排名。因此在读取完第一个考场的信息之后,就给第一个考场排序,然后按照排序给每个学生的排名赋值。最后在最外层来一个大排序,然后排名,就?。麻烦的是,数组的下标T~T。题目中规定的范围一定要看清楚!!!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;

struct Student{
	char id[13];   // 考号 ,题目要求 13位数字 
	int score;     // 成绩 
	int location;  // 考场 
	int rank;      // 校排名 
	int in_rank;   // 考场内排名 
};

// 成绩按递减顺序 
bool cmp(Student a,Student b){
	if(a.score != b.score){
		return a.score > b.score;
	}else{
		return strcmp(a.id,b.id)<0;
	} 
} 

int main(){
	int n ,k,total = 0;
	cin>>n; // n 条测试数据 ,即 n 个考场 
	vector<Student> t(30010); // 错误1:内存开的太小了,题目要求[N<=100,K<<300] 
	for(int i=0;i<n;i++){
		cin>>k;		// k 条,即 k 个考生/考场 
		for(int j=0;j<k;j++){
			cin>>t[total].id>>t[total].score;
			t[total].location = i+1; // 考场号 
			total++; // 学生总人数 total
		}
//		cout<<"i="<<i<<" k="<<k<<" total="<<total<<endl;
		sort(t.begin()+total-k,t.end(),cmp); // 直接对当前考场排序
		t[total - k].in_rank =1; // total - k 为每一个考场的第一个位置 
		for(int j = total-k+1;j < total;j++){
			if(t[j].score ==t[j-1].score){
				t[j].in_rank = t[j-1].in_rank;
			}else{
				t[j].in_rank = j+1-(total-k);
			}
		} 	
		// 打印效果 
//		for(int i=total-k;i<total;i++){
//			cout<<"in_sort:id="<<t[i].id<<" score:"<<t[i].score<<" rank:"<<t[i].rank<<" location:"<<t[i].location<<" in_rank:"<<t[i].in_rank<<endl; 
//		}
	} 
	cout<<total<<endl; 
	// 排序 
	sort(t.begin(),t.end(),cmp);
//	for(int i=0;i<total;i++){
//		cout<<"total_sort:id="<<t[i].id<<" score:"<<t[i].score<<" rank:"<<t[i].rank<<" location:"<<t[i].location<<" in_rank:"<<t[i].in_rank<<endl; 
//	}
//	
	// 排名(总+考场内) 
	t[0].rank = 1;
	for(int i=1;i<total;i++){
		t[i].rank = t[i-1].rank;
		if(t[i].score != t[i-1].score){
			t[i].rank = i+1;
		}
	}   
	// 打印最后结果
	for(int i=0;i<total;i++){
		cout<<t[i].id<<" "<<t[i].rank<<" "<<t[i].location<<" "<<t[i].in_rank<<endl; 
	} 

	return 0;
} 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值