程序倒是全部AC,但是不知道这么写有没有问题,核心就是使用一个变量记录上一轮交换完成后次数的值,本轮交换完成后如果次数没发生变化,那么就是排好序了
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, a[10005], c = 0, t=0;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
while (true)
{
for (int i = 1; i < n; i++)
{
if (a[i] > a[i + 1])
{
swap(a[i], a[i + 1]);
c++;
}
}
if(t!=c) t=c;
else break;
}
cout << c;
return 0;
}