CodeForces 686B Little Robber Girl's Zoo (构造冒泡排序)

题意:给定一排列,让你通过一个区间交换的方式,完成排序。

析:这个题说了,最多不能超过20000次,而 n 最大才100,那么冒泡排序复杂度为 n * n,才10000,肯定是可以的,所以我们就模拟冒泡排序。

代码如下:

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <set>
#include <cstdio>
#include <cstring>

using namespace std;
typedef long long LL;
const int maxn = 100 + 5;
int a[maxn];
int n;

int main(){
    scanf("%d", &n);
    for(int i = 0; i < n; ++i)  scanf("%d", &a[i+1]);
    for(int i = 1; i < n; ++i)
        for(int j = 1; j < n; ++j)  if(a[j] > a[j+1]){
            printf("%d %d
", j, j+1);
            swap(a[j], a[j+1]);
        }
    return 0;
}
原文地址:https://www.cnblogs.com/dwtfukgv/p/5645469.html