目录
降序的sort(left, right,comp),bool comp(lhs, rhs)
一、排序
例题 3.1 排序
对输入的n个数进行排序并输出。
输入描述:
输入的第一行包括一个整数n(1<=n<=100)。 接下来的一行包括n个整数。
输出描述:
可能有多组测试数据,对于每组数据,将排序后的n个整数输出,每个数后面都有一个空格。 每组测试数据的结果占一行。
输入:
4 1 4 3 2输出:
1 2 3 4
代码
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int array[100];
int main() {
int n;
while(scanf("%d\n",&n)!=EOF){
for(int i=0;i<n;++i){
scanf("%d",&array[i]);
}
sort(array,array+n);
for(int i=0;i<n;++i){
cout<<array[i]<<" ";
}
cout<<endl;
}
return 0;
}
sort函数
需要加 #include<algorithm>
降序的sort(left, right,comp),bool comp(lhs, rhs)
注意:comp只是函数指针,不需要调用,因此不加()
习题3.2 整数奇偶排序
输入10个整数,彼此以空格分隔。重新排序以后输出(也按空格分隔),要求: 1.先输出其中的奇数,并按从大到小排列; 2.然后输出其中的偶数,并按从小到大排列。
输入描述:
任意排序的10个整数(0~100),彼此以空格分隔。
输出描述:
可能有多组测试数据,对于每组数据,按照要求排序后输出,由空格分隔。 1. 测试数据可能有很多组,请使用while(cin>>a[0]>>a[1]>>...>>a[9])类似的做法来实现; 2. 输入数据随机,有可能相等。
输入:
4 7 3 13 11 12 0 47 34 98输出:
47 13 11 7 3 0 4 12 34 98
解体思路:
代码
#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int a,int b){
if(a%2==1&&b%2==1) return a>b;
if(a%2==0&&b%2==0) return a<b;
return a%2>b%2;
}
int main(){
int a[10];
while(cin>>a[0]){
for(int i=1;i<10;i++) cin>>a[i];
sort(a,a+10,cmp);
for(int i=0;i<10;i++) cout<<a[i]<<' ';
cout<<endl;
}
return 0;
}
例题3.2 成绩排序
用一维数组存储学号和成绩,然后,按成绩排序输出。
输入描述:
输入第一行包括一个整数N(1<=N<=100),代表学生的个数。 接下来的N行每行包括两个整数p和q,分别代表每个学生的学号和成绩。
输出描述:
按照学生的成绩从小到大进行排序,并将排序后的学生信息打印出来。 如果学生的成绩相同,则按照学号的大小进行从小到大排序。
输入:
3 1 90 2 87 3 92输出:
2 87 1 90 3 92
自定义类型Struct
代码
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct Student
{
int number;
int score;
}; //这个分号一定不能省略,缺了就会报错
Student arr[100];
bool compare(Student x,Student y)
{
if(x.score==y.score)
{
return x.number<y.number; //若x<y则true
}
else
return x.score<y.score;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++) //固定数量的循环用for就可以,不固定用while
{
scanf("%d%d",&arr[i].number,&arr[i].score);
}
sort(arr,arr+n,compare);
for(int i=0;i<n;++i)
{
cout<<arr[i].number<<" "<<arr[i].score<<endl;
}
return 0;
}
注意:
1、struct Student
{
int number;
int score;
}; //这个分号一定不能省略,缺了就会报错
2、for(int i=0;i<n;i++) //固定数量的循环用for就可以,不固定用while
例题3.3 成绩排序2
查找和排序
题目:输入任意(用户,成绩)序列,可以获得成绩从高到低或从低到高的排列,相同成绩
都按先录入排列在前的规则处理。示例:
jack 70
peter 96
Tom 70
smith 67从高到低 成绩
peter 96
jack 70
Tom 70
smith 67从低到高
smith 67
jack 70
Tom 70
peter 96输入描述:
注意一个case里面有多组样例,请用循环处理输入 输入多行,先输入要排序的人的个数,然后输入排序方法0(降序)或者1(升序)再分别输入他们的名字和成绩,以一个空格隔开。
输出描述:
按照指定方式输出名字和成绩,名字和成绩之间以一个空格隔开
示例1
输入:
3 0 fang 90 yang 50 ning 70输出:
fang 90 ning 70 yang 50示例2
输入:
3 1 fang 90 yang 50 ning 70 3 0 moolgouua 43 aebjag 87 b 67输出:
yang 50 ning 70 fang 90 aebjag 87 b 67 moolgouua 43说明:
第一组用例: 3 1 fang 90 yang 50 ning 70 升序排序为: yang 50 ning 70 fang 90 第二组降序为: aebjag 87 b 67 moolgouua 43
代码
#include <cstdio>
#include <algorithm>
using namespace std;
struct Student {
char name[50];
int score;
int seq;
};
bool comp0(Student lh, Student rh) {
if (lh.score < rh.score)
return true;
else if (lh.score == rh.score && lh.seq < rh.seq)
return true;
else
return false;
}
bool comp1(Student lh, Student rh) {
if (lh.score > rh.score)
return true;
else if (lh.score == rh.score && lh.seq < rh.seq)
return true;
else
return false;
}
int main() {
int n, m;
int seq = 0;
Student arr[1000];
while (scanf("%d%d", &n, &m) != EOF) {
for (int i = 0; i < n; i++) {
scanf("%s%d", &arr[i].name, &arr[i].score);
arr[i].seq = i;
seq++;
}
if (0 == m) {
sort(arr, arr + n, comp1);
} else if (1 == m) {
sort(arr, arr + n, comp0);
} else {
return 0;
}
for (int i = 0; i < n; i++) {
printf("%s %d\n", arr[i].name, arr[i].score);
}
}
}
注意:sort为快速排序,不稳定
用序号去模拟稳定排序
二、查找
例题3.4 查找x
输入一个数n,然后输入n个数值各不相同,再输入一个值x,输出这个值在这个数组中的下标(从0开始,若不在数组中则输出-1)。
输入描述:
测试数据有多组,输入n(1<=n<=200),接着输入n个数,然后输入x。
输出描述:
对于每组输入,请输出结果。
输入:
2 1 3 0复制输出:
-1
代码:
#include<iostream>
#include<cstdio>
using namespace std;
int maxn=200+10;
int main(){
int arr[maxn];
int n;
while(scanf("%d",&n)!=EOF){
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
int x;
int answer=-1;
scanf("%d",&x);
for(int i=0;i<n;i++){
if(arr[i]==x){
answer=i;
break;
}
}
printf("%d\n",answer);
}
return 0;
}
顺序查找的问题
估算时间复杂度的需求
一般题目要求一秒时间
例题3.5 二分查找
输入数组长度 n 输入数组 a[1...n] 输入查找个数m 输入查找数字b[1...m] 输出 YES or NO 查找有则YES 否则NO 。
输入描述:
输入有多组数据。 每组输入n,然后输入n个整数,再输入m,然后再输入m个整数(1<=m,n<=100)。
输出描述:
如果在n个数组中输出YES否则输出NO。
输入:
5 1 5 2 4 3 3 2 5 6输出:
YES YES NO
代码
#include<iostream>
#include<algorithm>
#include<unordered_map>
using namespace std;
int s[100];
bool harshtable[1000000];
unordered_map<int, bool>harshtb;
bool linearsearch(int n, int x) { //线性查找
for (int i = 0; i < n; i++) {
if (s[i] == x)return true;
}
return false;
}
bool binarysearch(int left, int right,
int x) { //二分查找,一定要先保证有序
sort(s + left, s + right + 1);
while (left <= right) {
int mid = left + (right - left) / 2; //避免计算溢出
if (s[mid] < x)return binarysearch(mid + 1, right, x);
else if (s[mid] > x)return binarysearch(left, mid - 1, x);
else return true;
}
return false;
}
int main(){
int n,m,x;
while(scanf("%d",&n)!=EOF){
for(int i=0;i<n;i++)scanf("%d",&s[i]);
scanf("%d",&m);
sort(s,s+n); //先排序
while(m--){
scanf("%d",&x);
//if(linearsearch(n,x))printf("YES\n");//线性
if(binarysearch(0,n-1,x))printf("YES\n");//自定义二分法
// int position=lower_bound(s,s+n,x)-s;
// if(position!=n&&s[position]==x)printf("YES\n");//自带二分法
else printf("NO\n");
}
}
return 0;
}
/*int main() {
int n, m, x;
while (scanf("%d", &n) != EOF) {
for (int i = 0; i < n; i++) {
scanf("%d", &s[i]);
//harshtable[s[i]+500000]=true;//为避免负数作为下标,给定偏移量
harshtb[s[i]] = true;
}
scanf("%d", &m);
while (m--) {
scanf("%d", &x);
//if(harshtable[x+500000])printf("YES\n");//自定义散列查找
if (harshtb[x])printf("YES\n"); //自带散列查找
else printf("NO\n");
}
}
return 0;
}*/
二分查找
注意:1、二分查找先排序,其中sort函数进行排序的时间复杂度为n*log2n
2、一定要把原来的mid淘汰掉,防止死循环
用map解决查找问题
findIndex.end() 不在容器内;findIndex.find(findNum)如果找不到元素则返回end
map的底层是一个二叉搜索树
在 windows系统会优化成AVL树
在linux系统会优化成红黑树
构建树需要的时间复杂度是O(NlogN)
查找树需要的时间复杂度是O(logN),所以用map与二分查找效率一样,但是会有一定的空间消耗