现在有一个n个证书的序列,交换两个相邻的数的位置直到整个序列按照升序排列,
那么将整个序列排好需要交换多少次?
例如 1,2,3,5,4 只需要交换一次即可。
第一行输入一个正整数n(n<100),表述数字序列元素个数,占一行;
接下来一行输入从1到n的n个整数序列,中间用空格隔开
输入:
4
4 3 2 1
输出:
6
/*************************************************************************
> File Name: SpecialExchange.c
> Author: Juntaran
> Mail: Jacinthmail@gmail.com
> Created Time: 2016年04月24日 星期日 19时26分01秒
************************************************************************/
#include <stdio.h>
#define BufferSize 1000
int SpecialExchange()
{
int a[BufferSize];
int n;
scanf("%d", &n);
int i = 0;
for( i=0; i<n; i++ )
{
scanf("%d", &a[i]);
}
int count = 0;
int temp = 0;
int j = 0;
for( i=1; i<n; i++ )
{
if( a[i] < a[i-1] )
{
int t = a[i];
for( j = i-1; j>=0 && a[j]>t; j-- )
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
count ++;
}
}
}
printf("%d\n", count);
return count;
}
int main()
{
SpecialExchange();
return 0;
}