#UVa:10327-Flip Sort

即是計算Bubble Sort的交換次數。

C++(0.080)

/*******************************************************/
/* UVa 10327 Flip Sort                                 */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2012/03/31                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
using namespace std;

int main(){
  int N;
  int flipsort[1005];
  int M;

  while( scanf( "%d", &N ) != EOF ){
    for( int i = 0 ; i < N ; i++ )
      scanf( "%d", &flipsort[i] );

    M = 0;
    for( int i = 0 ; i < N ; i++ )
      for( int j = N-1 ; j > i ; j-- )
        if( flipsort[j] < flipsort[j-1] ){
          swap( flipsort[j], flipsort[j-1] );
          M++;
        }

    printf( "Minimum exchange operations : %d\n", M );
  }
  return 0;
} 

發表迴響