一、排序
排序在机试中分布广泛,既是必须掌握的基本算法,也是学习其他大部分算法的前提和基础。
排序:
- 内定义数据类型
- 自定义数据类型
- 设计比较函数
- 定义大小
1 排序–华中科技大学
描述:
对输入的n个数进行排序并输出。
输入描述:
输入的第一行包括一个整数n(1<=n<=100)。 接下来的一行包括n个整数。
输出描述:
可能有多组测试数据,对于每组数据,将排序后的n个整数输出,每个数后面都有一个空格。 每组测试数据的结果占一行。
示例1
输入:
4
1 4 3 2
输出:
1 2 3 4
题解:
这是在机试中曾经出现的关于排序考点的一个真题。面对这样的考题,我们很快就会联想各种排序算法,如快速排序、归并排序等。因为C+内部已为大家编写了基于快速排序的函数sot,只需调用这个函数,便能轻易地完成排序。
sort(first, last, comp)函数有三个参数:
first:待排序序列的起始地址(包含起始地址,即闭区间)
last:待排序序列的结束地址(不包含结束地址,即开区间)
comp:为排序方式,不填写时默认为升序方式。
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAX_NUM = 100;
/**
* 排序--华中科技大学
* @return
*/
int main() {
int n;
cin >> n;
int num[MAX_NUM];
for (int i = 0; i < n; ++i) {
cin >> num[i];
}
/**
* sort(first, last, comp)函数有三个参数:
* first:待排序序列的起始地址
* last:待排序序列的结束地址(不包含结束地址,即开区间)
* comp:为排序方式,不填写时默认为升序方式。
*/
sort(num, num + n);
for (int j = 0; j < n; ++j) {
cout << num[j] << " ";
}
cout << endl;
return 0;
}
2 成绩排序–清华大学
描述:
用一维数组存储学号和成绩,然后,按成绩排序输出。
输入描述:
输入第一行包括一个整数N(1<=N<=100),代表学生的个数。 接下来的N行每行包括两个整数p和q,分别代表每个学生的学号和成绩。
输出描述:
按照学生的成绩从小到大进行排序,并将排序后的学生信息打印出来。 如果学生的成绩相同,则按照学号的大小进行从小到大排序。
示例1
输入:
3
1 90
2 87
3 92
输出:
2 87
1 90
3 92
题解:
本题是对一组基本类型进行排序,因此要先将这一组基本类型构建为一个结构体或者类,然后对结构体或者类进行排序。
排序规则:按成绩升序排序,成绩相同则按学号升序排序。
因此,我们自定义比较函数compare(param x, param y)
遇到新的排序规则时,当比较函数的返回值为true时,则该比较函数的第一个参数会排在第二个参数前面,即x排在y前面。
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAX_NUM = 100;
struct Student {
int number;
int score;
};
/**
* 自定义比较规则,升序排序
* @param x
* @param y
* @return
*/
bool compare(Student x, Student y) {
//成绩相等,则比较学号
if (x.score == y.score) {
/*
* 若x.number < y.number 成立,其结果为true,则x排在y前面
* 若x.number < y.number 不成立,其结果为false,则x排在y后面
*/
return x.number < y.number;
} else {
//成绩不等,则比较成绩
return x.score < y.score;
}
}
/**
* 成绩排序--清华大学
* @return
*/
int main() {
Student studentArr[MAX_NUM];
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> studentArr[i].number >> studentArr[i].score;
}
sort(studentArr, studentArr + n, compare);
for (int j = 0; j < n; ++j) {
cout << studentArr[j].number << " " << studentArr[j].score << endl;
}
return 0;
}
3 整数奇偶排序–北京大学
描述:
输入10个整数,彼此以空格分隔。重新排序以后输出(也按空格分隔),要求: 1.先输出其中的奇数,并按从大到小排列; 2.然后输出其中的偶数,并按从小到大排列。
输入描述:
任意排序的10个整数(0~100),彼此以空格分隔。
输出描述:
可能有多组测试数据,对于每组数据,按照要求排序后输出,由空格分隔。 1. 测试数据可能有很多组,请使用while(cin>>a[0]>>a[1]>>…>>a[9])类似的做法来实现; 2. 输入数据随机,有可能相等。
示例1
输入:
4 7 3 13 11 12 0 47 34 98
输出:
47 13 11 7 3 0 4 12 34 98
题解:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int NUM = 10;
/**
* 自定义排序规则
* 1.偶数在奇数前
* 2.偶数按大到小排序
* 3.奇数按小到大排序
* @return
*/
bool compare(int x, int y) {
if ((x & 1) == 0 && (y & 1) == 0) {
//x和y都是偶数,从小到大排序
return x < y;
} else if ((x & 1) == 1 && (y & 1) == 1) {
//x和y都是奇数,从大到小排序
return x > y;
} else {
//偶数排在奇数前
return (x & 1) > (y & 1);
}
}
/**
* 整数奇偶排序
* @return
*/
int main() {
int numArr[NUM];
while (cin >> numArr[0]) {
for (int i = 1; i < NUM; ++i) {
cin >> numArr[i];
}
sort(numArr, numArr + NUM, compare);
for (int j = 0; j < NUM; ++j) {
cout << numArr[j] << " ";
}
cout << endl;
}
return 0;
}
二、查找
查找也是一类必须掌握的基础算法,不仅会直接考察,也是其他算法的基础。同时,排序和查找也经常绑定在一起考察,排序的重要意义之一就帮助人们更加方便地查找。
1 查找–北京邮电大学
描述:
输入数组长度 n 输入数组 a[1…n] 输入查找个数m 输入查找数字b[1…m] 输出 YES or NO 查找有则YES 否则NO 。
输入描述:
输入有多组数据。 每组输入n,然后输入n个整数,再输入m,然后再输入m个整数(1<=m,n<=100)。
输出描述:
如果在n个数组中输出YES否则输出NO。
示例1
输入:
5
1 5 2 4 3
3
2 5 6
输出:
YES
YES
NO
题解:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAX_NUM = 100;
/**
* 线性查找
* @param arr
* @param n
* @param target
* @return
*/
int linearSearch(int arr[], int n, int target) {
for (int i = 0; i < n; ++i) {
if (arr[i] == target) {
return i;
}
}
return -1;
}
/**
* 自定义二分查找
* @param arr
* @param n
* @param target
* @return
*/
int binarySearch(int arr[], int n, int target) {
int left = 0;
int right = n - 1;
while (left <= right) {
/*
* 若mid = (left+right)/2 当left和right接近整形最大值时可能溢出
* 下面的写法效果相同且不会有溢出问题
*/
int mid = left + ((right - left) >> 1);
if (target < arr[mid]) {
right = mid - 1;
} else if (target > arr[mid]) {
left = mid + 1;
} else {
return mid;
}
}
return -1;
}
/**
* 查找--北京邮电大学
* @return
*/
int main() {
int numArr[MAX_NUM];
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> numArr[i];
}
/*
* 二分查找之前要先排序,顺序查找则不用
*/
sort(numArr, numArr + n);
int m;
cin >> m;
int target;
for (int j = 0; j < m; ++j) {
cin >> target;
if (binarySearch(numArr, n, target) != -1) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
}
此外,我们再来看看C++内置的二分查找函数。
lower_bound(): 返回大于或者等于目标值的第一个位置
upper_bound(): 返回大于目标值的第一个位置
序列 1 2 4 4 5 8 10 22
索引 0 1 2 3 4 5 6 7
以上述序列为例子
1.目标值为4
lower_bound(): 返回索引2的地址
upper_bound(): 返回索引4的地址
2.目标值为7
lower_bound(): 返回索引5的地址
upper_bound(): 返回索引5的地址
3.目标值为30
lower_bound(): 返回索引7的下一单元的地址(越界)
upper_bound(): 返回索引7的下一单元的地址(越界)
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAX_NUM = 100;
/**
* 查找--北京邮电大学
* @return
*/
int main() {
int numArr[MAX_NUM];
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> numArr[i];
}
/*
* 二分查找之前要先排序
*/
sort(numArr, numArr + n);
int m;
cin >> m;
int target;
for (int j = 0; j < m; ++j) {
cin >> target;
/*
* C++内部自带的二分查找
* lower_bound返回的是地址,因为减去首地址numArr,即可得到索引
*/
int position = lower_bound(numArr, numArr + n, target) - numArr;
if (position != n && numArr[position] == target) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
}
三、总结
本章学习了C++内置的sort()排序及其应用,同时也学习简单线性查找算法和二分查找算法。
参考教材:《王道计算机复试——机试指南》