补全9月11日至13日课评:为13名学生创建请假文档,为5个班级创建总结文档
This commit is contained in:
37
C++代码/CSP04/02-简单排序/01冒泡排序实现.cpp
Normal file
37
C++代码/CSP04/02-简单排序/01冒泡排序实现.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <bits/stdc++.h>
|
||||
#define endl '\n'
|
||||
using namespace std;
|
||||
|
||||
int n;
|
||||
int arr[100100];
|
||||
|
||||
void bubble(int n){
|
||||
for(int i = 1; i <= n - 1; i++){ // 趟次
|
||||
bool bol = true; // 认为排好序了
|
||||
for(int j = 1; j <= n - i; j++){
|
||||
if(arr[j] > arr[j+1]){
|
||||
swap(arr[j], arr[j+1]);
|
||||
bol = false;
|
||||
}
|
||||
}
|
||||
if(bol){
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
cin >> n;
|
||||
for(int i = 1; i <= n; i++){
|
||||
cin >> arr[i];
|
||||
}
|
||||
|
||||
bubble(n);
|
||||
|
||||
for(int i = 1; i <= n; i++){
|
||||
cout << arr[i] << " ";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
38
C++代码/CSP04/02-简单排序/02选择排序实现.cpp
Normal file
38
C++代码/CSP04/02-简单排序/02选择排序实现.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <bits/stdc++.h>
|
||||
#define endl '\n'
|
||||
using namespace std;
|
||||
|
||||
int n;
|
||||
int arr[100100];
|
||||
|
||||
void selection(int n) {
|
||||
for(int i = 1; i <= n; i++) {
|
||||
int maxx = arr[i]; // 当前这个数是最大的
|
||||
int idx = i;
|
||||
for(int j = i + 1; j <= n; j++) {
|
||||
// 打擂台
|
||||
if(arr[j] > maxx) {
|
||||
maxx = arr[j];
|
||||
idx = j;
|
||||
}
|
||||
}
|
||||
if(arr[i] != maxx) {
|
||||
swap(arr[i], arr[idx]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
cin >> n;
|
||||
for(int i = 1; i <= n; i++) {
|
||||
cin >> arr[i];
|
||||
}
|
||||
|
||||
selection(n);
|
||||
|
||||
for(int i = 1; i <= n; i++) {
|
||||
cout << arr[i] << " ";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
BIN
C++代码/CSP04/02-简单排序/02选择排序实现.exe
Normal file
BIN
C++代码/CSP04/02-简单排序/02选择排序实现.exe
Normal file
Binary file not shown.
36
C++代码/CSP04/02-简单排序/03计算排序后的交换次数.cpp
Normal file
36
C++代码/CSP04/02-简单排序/03计算排序后的交换次数.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <bits/stdc++.h>
|
||||
#define endl '\n'
|
||||
using namespace std;
|
||||
|
||||
int n, cnt = 0;
|
||||
int arr[100100];
|
||||
|
||||
void insertion(int n){
|
||||
for(int i = 2; i <= n; i++){ // i前面的都是有序的
|
||||
int curr = arr[i]; // 当前乱序的第一个
|
||||
int j = i - 1;
|
||||
while(j >= 1 && arr[j] > curr){
|
||||
cnt++;
|
||||
arr[j+1] = arr[j];
|
||||
j--;
|
||||
}
|
||||
j++;
|
||||
|
||||
arr[j] = curr;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
cin >> n;
|
||||
for(int i = 1; i <= n; i++) {
|
||||
cin >> arr[i];
|
||||
}
|
||||
|
||||
insertion(n);
|
||||
|
||||
for(int i = 1; i <= n; i++) {
|
||||
cout << arr[i] << " ";
|
||||
}
|
||||
cout << endl << cnt;
|
||||
return 0;
|
||||
}
|
||||
BIN
C++代码/CSP04/02-简单排序/03计算排序后的交换次数.exe
Normal file
BIN
C++代码/CSP04/02-简单排序/03计算排序后的交换次数.exe
Normal file
Binary file not shown.
35
C++代码/CSP04/02-简单排序/04求第 K 小的数.cpp
Normal file
35
C++代码/CSP04/02-简单排序/04求第 K 小的数.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <bits/stdc++.h>
|
||||
#define endl '\n'
|
||||
using namespace std;
|
||||
|
||||
int n, k;
|
||||
int arr[100100];
|
||||
|
||||
void bubble(int n){
|
||||
for(int i = 1; i <= n - 1; i++){ // 趟次
|
||||
bool bol = true; // 认为排好序了
|
||||
for(int j = 1; j <= n - i; j++){
|
||||
if(arr[j] > arr[j+1]){
|
||||
swap(arr[j], arr[j+1]);
|
||||
bol = false;
|
||||
}
|
||||
}
|
||||
if(bol){
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
cin >> n >> k;
|
||||
for(int i = 1; i <= n; i++){
|
||||
cin >> arr[i];
|
||||
}
|
||||
|
||||
bubble(n);
|
||||
|
||||
cout << arr[k];
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
BIN
C++代码/CSP04/02-简单排序/04求第 K 小的数.exe
Normal file
BIN
C++代码/CSP04/02-简单排序/04求第 K 小的数.exe
Normal file
Binary file not shown.
38
C++代码/CSP04/02-简单排序/05成绩排名.cpp
Normal file
38
C++代码/CSP04/02-简单排序/05成绩排名.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <bits/stdc++.h>
|
||||
#define endl '\n'
|
||||
using namespace std;
|
||||
|
||||
int n;
|
||||
string name[100100];
|
||||
int score[100100];
|
||||
|
||||
void bubble(int n){
|
||||
for(int i = 1; i <= n - 1; i++){ // ÌË´Î
|
||||
bool bol = true; // ÈÏΪÅźÃÐòÁË
|
||||
for(int j = 1; j <= n - i; j++){
|
||||
if(score[j] < score[j+1]){
|
||||
bol = false;
|
||||
swap(score[j], score[j+1]);
|
||||
swap(name[j], name[j+1]);
|
||||
}
|
||||
}
|
||||
if(bol){
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
cin >> n;
|
||||
for(int i = 1; i <= n; i++){
|
||||
cin >> name[i] >> score[i];
|
||||
}
|
||||
|
||||
bubble(n);
|
||||
|
||||
for(int i = 1; i <= n; i++){
|
||||
cout << name[i] << " " << score[i] << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
BIN
C++代码/CSP04/02-简单排序/05成绩排名.exe
Normal file
BIN
C++代码/CSP04/02-简单排序/05成绩排名.exe
Normal file
Binary file not shown.
Reference in New Issue
Block a user