竞赛章程:https://dasai.lanqiao.cn/notices/839/

oi赛制

C++自带的sort排序函数的使用

需要引用算法库

1
#include <algorithm>

通过写

1
sort(arr,arr+5)

我们可以将arr中从开始的元素到第5个元素按从小到大的顺序排列

而如果我们写

1
sort(arr+i,arr+j)

那么被排序的将是arr[i]到arr[j-1],其他元素将保持原位置

默认是从小到大排序,如果要从大到小排,则可以传入第三个参数–排序方法:

1
2
#include <functional>
sort(arr,arr+5,greater<int>());

其中,greater表示更大的意思,<int>表示待排序的数组中的元素类型为int

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int arr[]={2,4,5,3,1};
sort(arr,arr+5,greater<int>());
for(int i=0;i<5;i++){
printf("%d ",arr[i]);
}
return 0;
}

字符串的两种风格

1
2
3
4
5
//c风格字符串
char arr[]="hello world";
//c++风格字符串
#include <string> //在使用c++风格的字符串要包含这个头文件
string arr = "hello world"

c++读入字符串并打印

1
2
3
4
5
6
7
8
#include <string>
#include <iostream>
using namespace std;
int main(){
string arr = "hello world";
cout << arr << endl;
return 0;
}

c语言读入字符串并打印

1
2
3
4
5
6
7
8
9
#include <cstdio>
int main() {
char str[100];

printf("请输入字符串:");
scanf("%s", str);
printf("您输入的字符串是:%s\n", str);
return 0;
}

c语言读入整形数组元素并打印

需要注意的是scanf读入的时候遇到空白字符(空格、制表符、换行符等)或者文件结束符时停止读取,所以输入的时候数字要通过空格或者换行隔开

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<cstdio>
#include <functional>
#include <algorithm>
using namespace std;
int main(){
int arr[10];
for(int i=0;i<10;i++){
scanf("%d",&arr[i]);
}
sort(arr,arr+10,greater<int>());
for(int j=0;j<10;j++){
printf("%d ",arr[j]);
}
return 0;
}
1
cout << arr[j] << " "; //空格隔开

末尾严格控制空格

知道个数时,判断结尾

1
2
3
4
5
6
7
8
for(int i=1;i<=6;i++){
if(i!=6){
cout << B[i] << " ";
}else{
cout << B[i] << endl;
}
}
return 0;

不知道个数时,判断开头

1
2
3
4
5
6
7
for(int i=1;i<=6;i++){
if(i==1){
cout << B[i];
}else{
cout << " " << B[i];
}
}

在 C++ 的标准库中,sort 函数可以接受一个比较函数作为参数,以便在排序时确定元素的顺序。这个比较函数需要满足一定的条件,它应该接受两个参数(通常是被比较的两个元素),并返回一个布尔值,表示两个元素的比较结果。如果返回 true,则第一个参数应该排在第二个参数之前;如果返回 false,则第一个参数应该排在第二个参数之后

1
2
3
4
bool cmp(int x,int y){ //函数名可以随意
return x>y;
}
sort(arr,arr+10,cmp);

结构体赋初值

对于开发更加复杂的程序,我们往往会许哟啊构造函数在初始化的过程中做很多事情,但是对于竞赛 来说,我们只需要构造函数给结构体赋初值就行了,所以我们其实可以使用另一种更加简单的方式,初始化列表,来直接对结构体成员进行初始化:

1
2
3
4
5
6
struct student{
int score;
string name;
student() {} //注意不可省略默认构造函数
student(string n,ing s): name(n),score(s) {}
};

如上所示,初始化列表的写法是,在构造函数的括号后面加一个冒号,然后按照成员变量(参数)的格式,依次对每一个变量进行初始化,彼此之间用逗号隔开。

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;
struct student{
string name;
int score;
student() {}
student(string n,int s): name(n),score(s) {}
};
int main(){
student stu[3];
for(int i=0;i<3;i++){
int s;
string n;
cin >> n >> s;
stu[i]=student(n,s);
}
for(int i=0;i<3;i++){
cout << stu[i].name << " " << stu[i].score << endl;
}
return 0;
}

对结构体进行排序:

输入三个学生的四科分数并按照分数从高到低排序 ,先看第一科,再看第二科依次类推

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct student{
string name;
int score[4];
};
bool cmp(student x,student y){
if(x.score[0]!=y.score[0]){
return x.score[0]>y.score[0];
}
if(x.score[1]!=y.score[1]){
return x.score[1]>y.score[1];
}
if(x.score[2]!=y.score[2]){
return x.score[2]>y.score[2];
}
if(x.score[3]!=y.score[3]){
return x.score[3]>y.score[3];
}
}
int main(){
student stu[3];
for(int i=0;i<3;i++){
cin >> stu[i].name;
for(int j=0;j<4;j++){
cin >> stu[i].score[j];
}
}
sort(stu,stu+3,cmp);
for(int i=0;i<3;i++){
cout << stu[i].name << ": ";
for(int j=0;j<4;j++){
cout << stu[i].score[j] << " ";
}
cout << endl;
}
return 0;
}

排序函数的名字是可以随意命名的,也可以同时定义多个排序函数,根据实际情况调用

image-20240323172043832

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int N,l1,r1,l2,r2;
int score[105];
scanf("%d%d%d%d%d",&N,&l1,&r1,&l2,&r2);
for(int i=0;i<N;i++){
scanf("%d",&score[i]);
}
sort(score+l1-1,score+r1);
sort(score+l2-1,score+r2,greater<int>());
for(int i=0;i<N;i++){
if(i==0){
printf("%d",score[i]);
}else{
printf(" %d",score[i]);
}
}
return 0;
}

排序两个数的所有位相加的和从小到大,如果相同则比较原数字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool cmp(int a,int b){
int aa=a,bb=b;
int suma = 0,sumb = 0;
while(a){
suma+=a%10;
a/=10;
}
while(b){
sumb+=b%10;
b/=10;
}
if(suma ==sumb){
return aa<bb;
}
return suma <sumb;
}