2010年9月19日 星期日

ACM 299 - Train Swapping

#include <stdio.h>
#include <malloc.h>
int *S, size;

int bubbleSort ()
{
int i, j, tmp, count = 0;
for (i = 0; i < size; i ++)
{
for (j = 0; j < size - i - 1; j ++)
{
if (S[j] > S[j + 1])
{
tmp = S[j];
S[j] = S[j + 1];
S[j + 1] = tmp;
count ++;
}
}
}
return count;
}

int main()
{
/*freopen("111.txt", "r", stdin);
freopen("111w.txt", "w", stdout);*/
int n, i, j;
scanf("%d", &n);
for (i = 0; i < n; i ++)
{
scanf("%d", &size);

S = (int *)malloc(sizeof(int)*size);
for (j = 0; j < size; j ++)
scanf("%d", &S[j]);
printf("Optimal train swapping takes %d swaps.\n", bubbleSort());
free(S);
}
}

回目錄
回首頁

沒有留言 :

張貼留言

Related Posts Plugin for WordPress, Blogger...