课评: 转出学生归档(王子墨/张立坤/黄俊博)+ 周四班级总结 + 新生章梓皓档案 + C++示例代码
This commit is contained in:
42
C++代码/CSP04/02-简单排序/未命名1.cpp
Normal file
42
C++代码/CSP04/02-简单排序/未命名1.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <bits/stdc++.h>
|
||||
#define endl '\n'
|
||||
using namespace std;
|
||||
|
||||
int n;
|
||||
int arr[100100];
|
||||
|
||||
int main() {
|
||||
cin >> n;
|
||||
for(int i = 1; i <= n; i++) {
|
||||
cin >> arr[i];
|
||||
}
|
||||
|
||||
// 冒泡排序
|
||||
for(int i = 1; i <= n - 1; i++) { // i表示的是趟数
|
||||
bool ok = true; // 一开始我认为没有交换
|
||||
for(int j = 1; j <= n - i; j++) { // j表示的是食指 j+1
|
||||
if(arr[j] > arr[j+1]) {
|
||||
swap(arr[j], arr[j+1]);
|
||||
ok = false; // 交换过了
|
||||
}
|
||||
}
|
||||
|
||||
cout << "第" << i << "趟:";
|
||||
for(int i = 1; i <= n; i++) {
|
||||
cout << arr[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
|
||||
if(ok) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
cout << "最终:";
|
||||
for(int i = 1; i <= n; i++) {
|
||||
cout << arr[i] << " ";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user