K.Bro Sorting
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5122
解题思路:
坑死了,想了很多种方法,结果都超时了,不过队员告诉我一种很简单的方法:从后往前开始遍历,对于每一个数只要前面的数比它小就要交换一次,然后cnt++就行了,其实就是模拟冒泡排序。。。
AC代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[1000005];
int main(){
int T,t = 1;
scanf("%d",&T);
while(T--){
int x,n,cnt = 0;
scanf("%d",&n);
for(int i = 1; i <= n; i++)
scanf("%d",&a[i]);
for(int i = n-1; i >= 1; i--){
if(a[i] > a[i+1]){
cnt++;
swap(a[i],a[i+1]);
}
}
printf("Case #%d: %d\n",t++,cnt);
}
return 0;
}