作者在 2012-03-20 13:14:10 发布以下内容
Sort it
描述You want to processe a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. Then how many times it need.时间限制:1000 ms | 内存限制:65535 KB
难度:2
For example, 1 2 3 5 4, we only need one operation : swap 5 and 4.输入The input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 1000); the next line contains a permutation of the n integers from 1 to n.输出For each case, output the minimum times need to sort it in ascending order on a single line.样例输入3 1 2 3 4 4 3 2 1 样例输出0 6
#include <stdio.h>
int main()
{
int m,a[1000];
while(scanf("%d",&m)!=EOF)
{
int i,j,co=0,mi;
for(i=0;i<m;i++)
scanf("%d",&a[i]);
for(int flag=1,i=0;i<m&&flag;i++)//冒泡泡
for(flag=0,j=0;j<m-i-1;j++)
if(a[j]>a[j+1])
{
mi=a[j];
a[j]=a[j+1];
a[j+1]=mi;
flag=1;
co++; //记录冒了多少泡(嘿嘿)
}
printf("%d\n",co);
}
return 0;
}