题解 | #排序#
排序
https://www.nowcoder.com/practice/508f66c6c93d4191ab25151066cb50ef
就是排序问题
我写的是冒泡
#include <iostream>
using namespace std;
#define N 101
int main(){
int n;
while(cin >> n){
int a[N];
for (int i = 0; i < n; i++){
cin >> a[i];
}
for (int i= 0; i < n-1;i++){
int flag = 0;
for (int j = 0; j < n-i-1;j++){
if (a[j]>a[j+1]){
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
flag = 1;
}
}
if (!flag) break;
}
for (int i = 0; i < n; i++){
cout <<a[i]<<" ";
}
}
}
查看18道真题和解析
